Tuesday, March 8, 2016

Create Header and Footer in Android

Create Header and footer with the help of xml only :

    main.xml :

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <!-- HEADER -->
    <include android:id="@+id/top_header"
       layout="@layout/layout_header"
        android:layout_alignParentTop="true" />
    <!-- FOOTER -->
    <LinearLayout 
       android:id="@+id/bottom_menu"
        android:layout_width="fill_parent"
       android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">
        <!-- menu bar --> 
       <include layout="@layout/layout_footer_menu" />
    </LinearLayout>
   <!-- MAIN PART -->
    <LinearLayout 
       android:id="@+id/sub_content_view"
        android:layout_width="fill_parent"
       android:layout_height="fill_parent"
        android:layout_above="@id/bottom_menu" 
       android:layout_below="@id/top_header"
        android:layout_weight="1"
       android:background="#EAEAEA" 
       android:orientation="vertical"
        android:paddingBottom="5sp">
</LinearLayout>
</RelativeLayout>


    You can create your own header and footer layout and inflate inside the main xml layout with the help of <include /> tag in xml layout.
    ---------------------------------------OR---------------------------------------------
    You could create Header and footer using Relative layout as :

 <?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">
/* HEADER */
<RelativeLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:background="#48fff7"
    android:paddingTop="8dp">
</RelativeLayout>
/* BODY */
<RelativeLayout
   android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/footer"
    android:layout_below="@+id/header">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.8">
        <TextView        
        android:id="@+id/textView7" 
           android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingLeft="10dp"
            android:text="BODY" 
          android:textAppearance="?android:attr/textAppearanceLarge"
           android:textColor="#fff" />
    </LinearLayout>
</RelativeLayout>
/* FOOTER */
<RelativeLayout
    android:id="@+id/footer"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="10dp"
    android:background="#48fff7">
    <LinearLayout
        android:layout_width="match_parent"
       android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" 
       android:weightSum="1">
    </LinearLayout>
</RelativeLayout>
</RelativeLayout>

For futher any information or doubt please let me know..?

No comments:

Post a Comment