How to include layout inside layout in Android?
I am creating common layout. I want to include that layout in another page.
To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.
By the term of Nested we mean one Layout inside of other Layout. In Android all layout can be nested one another. In this example we create a Registration Form with multiple fields using Nested Linear Layouts.
We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.
You can declare a layout in two ways: Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.
Edit: As in a comment rightly requested here some more information. Use the include
tag
<include android:layout_width="match_parent" android:layout_height="wrap_content" layout="@layout/yourlayout" />
to include the layout you want to reuse.
Check this link out...
Note that if you include android:id...
into the <include />
tag, it will override whatever id was defined inside the included layout. For example:
<include android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/some_id_if_needed" layout="@layout/yourlayout" />
yourlayout.xml:
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/some_other_id"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button1" /> </LinearLayout>
Then you would reference this included layout in code as follows:
View includedLayout = findViewById(R.id.some_id_if_needed); Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1);
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