Replace `layout` of an <include/> tag programmatically for Android

Just change the code in the respective java file (e.g. MainActivity.java):

// Retrieve layout:
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_container); 

// Instantiate & use inflater:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_layout, null);

// Clear & set new views:
mainLayout.removeAllViews();
mainLayout.addView(layout);

It worked for me.

Tags:

Xml

Android