what is the difference between Button button=(Button) findViewbyID(R.id.button); and Button button = new Button; in Android? -


what difference between button button= (button) findviewbyid(r.id.button); , button button = new button(this);?

there 2 approach create , set button.

  1. by using xml file:

in activity.xml write code this:

    <button         android:id="@+id/button"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="press" /> 

then have set it's reference in activity class file this:

    button button=(button) findviewbyid(r.id.button); 
  1. programatically inside activity class like:

    button mybutton = new button(this); mybutton.settext("press"); linearlayout layout = (linearlayout)findviewbyid(r.id.buttonlayout); layoutparams lp = new layoutparams(layoutparams.match_parent, layoutparams.wrap_content); layout .addview(mybutton, lp ); 

Comments