15

how to add one layout in another layout.I created this layout in tblrow.XML.so i want to add this rows in menu.XML.i want to add this rows depending upon the no of rows.How can i do that.If i add it how can i identify each row.Please help me.I need solution in java not in XML. My Code is

<TableRow android:id="@+id/tblRowMovies" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/btn_backgrnd"  android:clickable="true" android:focusable = "true" android:layout_weight="1.0">
<ImageView android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/movie" android:layout_gravity="center_vertical|center_horizontal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MOVIES" android:layout_gravity="center_vertical" android:paddingLeft="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold">
</TextView>        

.

VK.Dev
  • 801
  • 4
  • 11
  • 24

2 Answers2

27

You can include one layout file in another and this technique is called reusing Android layouts using include tag. e.g:

<include android:id="@+id/myid1" layout="@layout/workspace_screen" />

This is explained well in a blogpost by Android developers..

Yet another article on the Android developer site explains the layout re-usability.

Gopinath
  • 12,981
  • 6
  • 36
  • 50
4

Create file titlebar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width=”match_parent”
    android:layout_height="wrap_content"
    android:background="@color/titlebar_bg">

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:src="@drawable/gafricalogo" />
</FrameLayout>

In main layout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/titlebar"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>
d.danailov
  • 9,594
  • 4
  • 51
  • 36