How to Add button using code dynamically in android
Main.xml
Main.java
Main.xml
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</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);