81

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?

3
  • 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 and also here. Commented Sep 25, 2013 at 8: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. Commented Sep 25, 2013 at 8:19
  • OK. So if you want to change the "layout" paramater, kcoppock's answer is suitable. Commented Sep 25, 2013 at 8:23

4 Answers 4

180

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();
Sign up to request clarification or add additional context in comments.

11 Comments

@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
@kcoppock and how i can change view after button click ( i have 4 view )
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();
Unless you immediately want to do something with the inflated result.
any another idea to make ViewStub invisible/visible programmatically..??
|
6
ViewStub stub = (ViewStub) findViewById(R.id.text_post);
        stub.setLayoutResource(R.layout.profile_header);
        View inflated = stub.inflate();

1 Comment

how will you use the views inside profile_header.xml ?
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();

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.