Thursday, 6 June 2013

Include tag in android



      Hi guys Today we will see include tag of android xml

This is one of the best thing that we can reuse existing xml file in other xml.
so here is good example,

home_page.xml

just create xml as bleow Assume that this top_layout.xml  is a  top bar of each page of app. 

top_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/genericbar_background_tile" >

    <Button
        android:id="@+id/BackBtn"
        style="@style/K_style"
        android:layout_alignParentLeft="true"
        android:text="@string/backBtn" />

    <Button
        android:id="@+id/HomeBtn"
        style="@style/K_style"
        android:layout_alignParentRight="true"
        android:text="@string/HomeStr" />

    <TextView
        android:id="@+id/titleTv"
        style="@style/K_style"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/titleStr"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>





To include this layout in our hompage or any page just add the
    <include layout="@layout/ layoutname"/>



home_page.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include layout="@layout/top_lyaout"/>
  
    <LinearLayout
        android:id="@+id/TopLNLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/MidleLNLaoyout"
        android:layout_centerHorizontal="true" >

        <Button
            android:id="@+id/TopleftBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/TopRightBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/MidleLNLaoyout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <Button
            android:id="@+id/MidleftBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/MidRightBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/BottonLNLaout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MidleLNLaoyout"
        android:layout_centerHorizontal="true" >

        <Button
            android:id="@+id/BottomLeftBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/BottomRightBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


</RelativeLayout>

So final screen will be look like this as top_Layout.xml included in home-page xml. 
this can be reuse in any number of screens.

                               


I hope It might be help full,
Happy Codding :)