How to include layout inside layout?
Solution 1
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...
Solution 2
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);
Solution 3
Use <include />
tag.
<include
android:id="@+id/some_id_if_needed"
layout="@layout/some_layout"/>
Also, read Creating Reusable UI Components and Merging Layouts articles.
Solution 4
Try this
<include
android:id="@+id/OnlineOffline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="@layout/YourLayoutName" />
Solution 5
From Official documents about Re-using Layouts
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.
Here is my header.xml file which i can reuse using include tag
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="@string/app_name"
android:textColor="#000000" />
</RelativeLayout>
No I use the tag in XML to add another layout from another XML file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0" >
<include
android:id="@+id/header_VIEW"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#ffffff"
android:orientation="vertical"
android:padding="5dp" >
</LinearLayout>
Related videos on Youtube
mohan
Updated on September 06, 2020Comments
-
mohan over 3 years
How to include layout inside layout in Android?
I am creating common layout. I want to include that layout in another page.
-
Jimmy about 10 yearsBoth of the links does not exist any more.
-
Agna JirKon Rx almost 7 yearsjust a tiny detail: use android:layout_width="match_parent" instead of android:layout_width="fill_parent". fill_parent is deprecated.
-
Florian Walther about 6 yearsWhy put the TextView in a RelativeLayout and not as the root view?
-
IntelliJ Amiya about 6 years@FlorianWalther This is an example
-
Florian Walther about 6 yearsThanks for the quick response. But am I right that I could put the TextView as the root element or am I missing something? Because I want to reuse a ProgressBar and wonder if I have to put it into a layout.
-
IntelliJ Amiya about 6 years@FlorianWalther
Because I want to reuse a ProgressBar
what problem coming? -
Florian Walther about 6 yearsThere is no problem, it works. But all the examples I see online put the single widget into another layout and I wonder why.
-
JohnyTex almost 5 yearsCan I include a layout and set some of its properties via the xml, e.g. set a text string in the sublayout directly in the <include> tag?
-
Mousa Halaseh over 4 years@JohnyTex Not sure if you can do it directly in the
<include />
tag, however, you can do it using java code. see Phileo99's answer below to know how to get a reference to the included layout. and then you can alter it's content. -
Fattie about 3 yearsNote, there is NO NEED to include the layout items, in, the include tag. You can just have them in the "included body" (the sub file) and it works perfectly just the same.