Search This Blog

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);

No comments:

Post a Comment