Search This Blog

Showing posts with label Button. Show all posts
Showing posts with label Button. Show all posts

Wednesday, 21 January 2015

Add button using code dynamically in android

How to Add button using code dynamically in android

Main.xml

<LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

</LinearLayout>


Main.java


LinearLayout myLayout = (LinearLayout) findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
myLayout.setOrientation(LinearLayout.VERTICAL);
  
Button button = new Button(this);
button.setText("B1");
myLayout.addView(button, params);