8

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
  • 17,220
  • 36
  • 109
  • 159
  • Possible solution to your "update": http://stackoverflow.com/questions/3421864/how-do-i-access-the-views-inside-the-layout-when-i-reuse-it-multiple-times – Haozhun Jul 24 '13 at 14:42

3 Answers3

7

See this doc reusing layouts.

xevincent
  • 3,674
  • 18
  • 20
  • @ 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 ? – Leem Jul 08 '11 at 07:52
  • 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)). – xevincent Jul 08 '11 at 07:58
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" />
Arnaud
  • 17,268
  • 9
  • 65
  • 83
0

Have a look on this doc, Link updated

http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html

Walid Hossain
  • 2,724
  • 2
  • 28
  • 39