Android: How to dynamically include a XML Layout? Android: How to dynamically include a XML Layout? android android

Android: How to dynamically include a XML Layout?


<RelativeLayout android:id="@+id/rl" ...

In your code:

// get your outer relative layoutRelativeLayout rl = (RelativeLayout) findById(R.id.rl);// inflate content layout and add it to the relative layout as second child// add as second child, therefore pass index 1 (0,1,...)LayoutInflater layoutInflater = (LayoutInflater)         this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); 


You could try using a ViewStub and just change which layout it is going to inflate programatically.

This answer discusses using one ViewStub.