How to include layout inside layout?

133,079

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>
Share:
133,079

Related videos on Youtube

mohan
Author by

mohan

Updated on September 06, 2020

Comments

  • mohan
    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
    Jimmy about 10 years
    Both of the links does not exist any more.
  • Agna JirKon Rx
    Agna JirKon Rx almost 7 years
    just a tiny detail: use android:layout_width="match_parent" instead of android:layout_width="fill_parent". fill_parent is deprecated.
  • Florian Walther
    Florian Walther about 6 years
    Why put the TextView in a RelativeLayout and not as the root view?
  • IntelliJ Amiya
    IntelliJ Amiya about 6 years
    @FlorianWalther This is an example
  • Florian Walther
    Florian Walther about 6 years
    Thanks 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
    IntelliJ Amiya about 6 years
    @FlorianWalther Because I want to reuse a ProgressBar what problem coming?
  • Florian Walther
    Florian Walther about 6 years
    There is no problem, it works. But all the examples I see online put the single widget into another layout and I wonder why.
  • JohnyTex
    JohnyTex almost 5 years
    Can 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
    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
    Fattie about 3 years
    Note, 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.

Related