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?
Use the <include> tag However, if you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.
Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the tag to embed another layout inside the current layout.
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();
ViewStub stub = (ViewStub) findViewById(R.id.text_post); stub.setLayoutResource(R.layout.profile_header); View inflated = stub.inflate();
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