Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed one layout inside another in my case?

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 ?

like image 369
Leem Avatar asked Jul 08 '11 07:07

Leem


2 Answers

See this doc reusing layouts.

like image 156
xevincent Avatar answered Sep 28 '22 09:09

xevincent


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" />
like image 23
Arnaud Avatar answered Sep 28 '22 11:09

Arnaud