How to embed one layout inside another in my case?

13,054

Solution 1

See this doc reusing layouts.

Solution 2

Just upvote xevincent's anwser. I added this answer because SO recommends to "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline."

So, basically, his link explains that you should use <include />.

<com.android.launcher.Workspace
    android:id="@+id/workspace"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    launcher:defaultScreen="1">

    <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
    <include android:id="@+id/cell2" layout="@layout/workspace_screen" />
    <include android:id="@+id/cell3" layout="@layout/workspace_screen" />

</com.android.launcher.Workspace>

And know that you can override the layout parameters:

<include android:layout_width="fill_parent" layout="@layout/image_holder" />
Share:
13,054

Related videos on Youtube

Leem
Author by

Leem

Updated on June 04, 2022

Comments

  • Leem
    Leem almost 2 years

    If I have a layout called bottom.xml,

    bottom.xml:(simply contain a textview and edit text view)

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical"
         >
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal" 
                android:text="@string/username"
    
            />
    
            <EditText 
                android:id="@+id/name"
    
                android:layout_width="120dip"
                android:layout_height="50dip"
                android:layout_gravity="center_horizontal"     
            />
     </LinearLayout>
    

    Is there any way to embed the above bottom.xml layout inside other layouts instead of repeatly writing the same code in several layout files (when other layouts have a part which contains the same layout as bottom.xml)?

    For example, if my admin.xml layout also contain part of the layout which looks exactly the same as bottom.xml, how to just embed the bottom.xml inside admin.xml instead of writing the same code again?

    admin.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
         >
         ...
         ...
            <!--How to embed bottom.xml here-->
         ...
     </LinearLayout>
    

    If there is no way to do it in Android, what could be the workaround??

    ----------Update-----------

    Like @xevincent suggested, I can reuse the bottom.xml by use <include> tag,

    But How to change the id of the elements inside the resued layout?

    For example, insdie bottom.xml, I would like to change the id of <editText android:id="@+id/name"> to <editText android:id="@+id/other_name"> when I reuse the bottom.xml layout in other layout, how to change the id ?

  • Leem
    Leem over 12 years
    @ xevincent , How to change the id of the elements inside the resued layout e.g. bottom.xml, I would like to change the id of editText when I reuse the bottom.xml layout in other layout, how to change the id ?
  • xevincent
    xevincent over 12 years
    You can't change the id of the elements inside bottom.xml in the xml way but you can do it in Java (See View.setId(int)).