Android: How to load layout xml files dynamically during runtime

Here is a sample code to load XML layout files at runtime within your Android application. Basically, you have to have a Layout tag within your main XML file so that we get to have a placeholder to where we want to store our content.

Then in your code, follow this:

where a is the class that extends Activity. If you are going to place this code within your class that extends Activity, you don’t need to include the a. in the code. Of course, this does not restrict to LinearLayout alone. You can use all other layouts to add new Views as its content.

Related Posts Plugin for WordPress, Blogger...


  1. This line..
    LinearLayout ll = (LinearLayout) a.findViewById(;
    Should be..
    LinearLayout ll = (LinearLayout) a.findViewById(;

    Otherwise it wont work.

    1. @yogibali: it’s in my post “where a is the class that extends Activity”. you can remove ‘a’ if the code is within your activity class

Leave a Reply

Your email address will not be published. Required fields are marked *