How can I programmatically include layout in Android?

53,542

Solution 1

Use a ViewStub instead of include:

<ViewStub
    android:id="@+id/layout_stub"
    android:inflatedId="@+id/message_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.75" />

Then in code, get a reference to the stub, set its layout resource, and inflate it:

ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(R.layout.whatever_layout_you_want);
View inflated = stub.inflate();

Solution 2

ViewStub stub = (ViewStub) findViewById(R.id.text_post);
        stub.setLayoutResource(R.layout.profile_header);
        View inflated = stub.inflate();

Solution 3

In Mono.Droid / Xamarin this worked for me:

ViewStub stub = FindViewById<ViewStub>(Resource.Id.layout_stub);
stub.LayoutResource = Resource.Layout.whatever_layout_you_want;
stub.Inflate();
Share:
53,542
slama007
Author by

slama007

Dream as if you'll live forever. live as if you'll die today. ;)

Updated on July 05, 2022

Comments

  • slama007
    slama007 almost 2 years

    I'm looking for a way to include a layout programmatically instead of using the XML tag include like in my example:

      <include layout="@layout/message"  
               android:layout_width="match_parent" 
               android:layout_height="match_parent" 
               android:layout_weight="0.75"/>
    

    Need to change this parameter "layout="@layout/message" programmatically, please.

    Any idea how to do this?

  • slama007
    slama007 over 10 years
    @kcoppock thanks it's ok but how i call the TextView in my layout because with this code TextView close1 = (TextView) findViewById(R.id.close1); i have error
  • slama007
    slama007 over 10 years
    @kcoppock and how i can change view after button click ( i have 4 view )
  • Alejandro Casanova
    Alejandro Casanova over 10 years
    No need to keep the inflated view in a separate variable, the layout is added to the original one so the components can de retrieved using findViewById as usual, so just call stub.inflate();
  • Kevin Coppock
    Kevin Coppock over 10 years
    Unless you immediately want to do something with the inflated result.
  • Umang Kothari
    Umang Kothari about 10 years
    any another idea to make ViewStub invisible/visible programmatically..??
  • Zapnologica
    Zapnologica almost 9 years
    Very useful tool. Made my life much easier and my code way simpler.
  • usr30911
    usr30911 almost 8 years
    i want to load multiple layouts in a sequence,once i load the first layout when i go to load the second layout i get stub as null, isnt the layout being loaded within the Viewstub so shouldnt i be able to access the parent viewstub?
  • Ahmad Vatani
    Ahmad Vatani almost 8 years
    @kcoppock i got The specified child already has a parent. You must call removeView() on the child's parent first error, can you help me?
  • Mohit Singh
    Mohit Singh over 7 years
    @UmangKothari inflated.setVisibility(View.VISIBLE); & inflated.setVisibility(View.INVISIBLE); works fine for me...
  • Arnold Brown
    Arnold Brown about 5 years
    How to initiate a view in included(stub) layout? - when try to initiate as normal process - null pointer exception ? any help
  • Arnold Brown
    Arnold Brown about 5 years
    how will you use the views inside profile_header.xml ?
  • Abdul Waheed
    Abdul Waheed over 4 years
    Exactly the thing which I was looking for