I want to change the layout
of my <include/>
tag dynamically/programmatically.
I have a main layout
that I want to re-use but the contents should change dynamically.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white_background"
android:orientation="vertical" >
<include
android:id="@+id/main_container"
layout="REPLACE_THIS" />
</android.support.v4.widget.SwipeRefreshLayout>
Above is the xml that I am using, I am assuming that you will need to find the id of the include and then change it programmatically.
Thanks in advance.
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.
I found the answer a couple of months ago. You just need to use a ViewStub and inflate the appropriate one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With