79

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?

Daniele D.
  • 2,624
  • 3
  • 36
  • 42
slama007
  • 1,273
  • 2
  • 18
  • 34
  • As far as I understand from your question, you are looking a way to change paramaters of your layout programatically, so here is the link : [here](http://stackoverflow.com/questions/15501831/add-layout-with-parameters-programmatically-android) and also [here](http://stackoverflow.com/questions/7470318/setting-parameter-of-layout-of-android-programmatically). – denizt Sep 25 '13 at 08:18
  • You can't really do it like that. What you can do is inflate the layout in-code and add the inflated view to your parent layout. Or you can use Fragments. – Lukas Knuth Sep 25 '13 at 08:19
  • OK. So if you want to change the "layout" paramater, kcoppock's answer is suitable. – denizt Sep 25 '13 at 08:23

4 Answers4

178

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();
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 1
    @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 Sep 25 '13 at 09:22
  • @kcoppock and how i can change view after button click ( i have 4 view ) – slama007 Sep 25 '13 at 09:41
  • 1
    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(); – Alejandro Casanova Jan 28 '14 at 02:52
  • Unless you immediately want to do something with the inflated result. – Kevin Coppock Jan 28 '14 at 02:57
  • 1
    any another idea to make ViewStub invisible/visible programmatically..?? – Umang Kothari Apr 07 '14 at 11:54
  • Very useful tool. Made my life much easier and my code way simpler. – Zapnologica May 29 '15 at 13:00
  • 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? – usr30911 May 25 '16 at 15:02
  • @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? – Ahmad Vatani Jun 26 '16 at 08:14
  • @UmangKothari `inflated.setVisibility(View.VISIBLE);` & `inflated.setVisibility(View.INVISIBLE);` works fine for me... – Mohit Singh Nov 27 '16 at 03:45
  • How to initiate a view in included(stub) layout? - when try to initiate as normal process - null pointer exception ? any help – Arnold Brown Apr 15 '19 at 06:46
  • Exactly the thing which I was looking for – Abdul Waheed Oct 09 '19 at 14:02
6
ViewStub stub = (ViewStub) findViewById(R.id.text_post);
        stub.setLayoutResource(R.layout.profile_header);
        View inflated = stub.inflate();
patel135
  • 927
  • 13
  • 19
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();
Daniele D.
  • 2,624
  • 3
  • 36
  • 42
1

ViewStub Kotlin Implementation

Inside your onCreate function:

val viewStub: ViewStub? = binding?.viewStub
viewStub?.layoutResource = R.layout.your_layout

if (viewStub?.parent != null) {
    viewStub.inflate()
}

Don't forget to comment for any inquiries

AI Shakil
  • 1,015
  • 10
  • 20