Normally the views defined using the XML layout field can be simply inflated by using the setContetntView(R.layout.main).
The following code is used to add a button dynamically: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Inflate the layout file res\layout\activity_uc.xml LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.main,null); //Initiate a new button Button reset = new Button(this); reset.setText("Reset Form"); //Add the new button to the layout layout.addView(reset,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); //Attach the view to the window setContentView(layout); }
Interface created using a Layout XML
Interface created after adding a button dynamically