How to add header and Footer to each activity in android

21,817

Solution 1

See this link :

It's exactly like your question. If you want to have these header and footer you should build a custom View and use it in your application. You can use something like action bar as your header.

Solution 2

This is best example for Common Header Footer in All Activities


BaseActiivty.java
=================



 public class BaseActivity extends FragmentActivity {

    RelativeLayout mRelativeLayout;
    FrameLayout frame_container;
    TextView header_txt,footer_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


    }

    @Override
    public void setContentView(int layoutResID)

    {
        mRelativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
        frame_container = (FrameLayout) mRelativeLayout.findViewById(R.id.frame_container);
        // set the drawer dialog_view as main content view of Activity.
        setContentView(mRelativeLayout);
        // add dialog_view of BaseActivities inside framelayout.i.e. frame_container
        getLayoutInflater().inflate(layoutResID, frame_container, true);

        header_txt = (TextView) findViewById(R.id.header_txt);
        footer_txt = (TextView) findViewById(R.id.footer_txt);

    }
}


   MainActivity.java
   =================

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }





activity_base.xml
=================





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


    <RelativeLayout
        android:id="@+id/header_RL"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorAccent">

        <TextView
            android:id="@+id/header_txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="30dp"
            android:gravity="center"
            android:text="Header"/>

    </RelativeLayout>


    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/footer_RL"
        android:layout_below="@+id/header_RL">

    </FrameLayout>


    <RelativeLayout
        android:id="@+id/footer_RL"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent">

        <TextView
            android:id="@+id/footer_txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="30dp"
            android:gravity="center"
            android:text="Footer"/>

    </RelativeLayout>

</RelativeLayout>





activity_main.xml
==================


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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Hello World!"
            android:textSize="30dp" />

    </LinearLayout>
</RelativeLayout>

Solution 3

Android does not have the concept of Header and Footer per se. You could however, define the conceptual headers and footers in your layouts once and then use them many times in other layouts simply by calling them using the (for example):

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

You can give this example a look to better understand how to re-use layouts throughout your application.

http://developer.android.com/training/improving-layouts/reusing-layouts.html

Solution 4

"do i need to define two header.xml or footer.xml and use these xml files in each of other xml files"

Yes, as far as I know this is the best way to do it. You can use the include xml tag to include other .xml layout files in other layout files. Like:

...
<include layout="@layout/header"/>
...
<include layout="@layout/footer"/>
...

Solution 5

You have two options. Include and Merge.

Please read more about these options here for include and here for merging

Share:
21,817
Vikas Gupta
Author by

Vikas Gupta

A software engineer having good knowledge of java working in different development department. :( So overall a learning phase going on

Updated on July 09, 2022

Comments

  • Vikas Gupta
    Vikas Gupta almost 2 years

    I want to add a ImageButton, a button and a textview in each of my activity at top and bottom.I thought of using header and footer. So I want to add a header and footer in each of my Android Activity. I don't have any idea of how to do that. I don't need source code of how to write a header or footer. What i want to know is where i have to define that header and footer means do i need to add a header and footer in each xml file or do i need to define two header.xml or footer.xml and use these xml files in each of other xml files. Or is there any other way mean like using a reference from the java file of that activity. Any help Appreciated.