3

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.

Vikas Gupta
  • 1,530
  • 5
  • 21
  • 34

6 Answers6

10

Define two separate files header.xml and footer.xml and and than use

`

<include layout="@layout/footer"/>
Sirko
  • 72,589
  • 19
  • 149
  • 183
4

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.

Community
  • 1
  • 1
AliSh
  • 10,085
  • 5
  • 44
  • 76
2

"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"/>
...
Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101
2

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

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • Thanks for help, now i'll try for that :) – Vikas Gupta Apr 24 '12 at 10:21
  • 1
    is it must to use relative layout for header footer – Vikas Gupta Apr 24 '12 at 10:22
  • Nope. That is just an example. I usually do use RelativeLayout (they are better than LinerLayouts). I have used them in LinearLayouts too. But I suppose, for the sake of understanding the example quicker, they have used the RelativeLayout. – Siddharth Lele Apr 24 '12 at 10:26
  • if it doesn't bother you, would you like to enlighten me on difference between Linear Layout and Relative Layout. or why Relative Layout is better? – Vikas Gupta Apr 24 '12 at 10:34
  • 2
    In LinerLayouts, all elements that are a part of your layout are laid out next to each other. The only way you can manipulate the position is by setting the proprieties such as orientation. Relativelayouts on the other hand, places elements "relative" to each other and IMHO, provides flexibility like no other. For example, from this link, http://developer.android.com/resources/tutorials/views/hello-relativelayout.html, an EditText with the id "@+id/entry" is placed below a TextView with the id "@id+/label" by this simple property: android:layout_below="@id/label" – Siddharth Lele Apr 24 '12 at 10:41
  • Follow this example for RelativeLayouts: http://developer.android.com/resources/tutorials/views/hello-relativelayout.html – Siddharth Lele Apr 24 '12 at 10:42
  • The second para reads "A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested ViewGroups. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout." – Siddharth Lele Apr 24 '12 at 10:43
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>
Rishi
  • 153
  • 2
  • 15
1

You have two options. Include and Merge.

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

Manuel
  • 10,153
  • 5
  • 41
  • 60
krishnakumarp
  • 8,967
  • 3
  • 49
  • 55