12

Many popular apps like Google Maps, Facebook, Foursquare, etc. have header and/or footer bars on most of their activities. These headers often include very useful buttons, and I would like to create one for my app. Does anyone know how they are done? I haven't been able to find anything so far.

Here are some pictures of what I mean: http://www.flickr.com/photos/calebgomer/6262815430 http://www.flickr.com/photos/calebgomer/6262815458

Caleb Gomer
  • 121
  • 1
  • 1
  • 5

4 Answers4

15

Using this way you can make your header-footer xml and use it to any of your activity also you just need to write code for the controls in header-footer once in HeaderFooter.java and can access it your project.

Build your HederFooter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:weightSum="10"
    android:id="@+id/commonlayout" android:background="#FFFFFF">
    <LinearLayout android:id="@+id/llheader"
        android:layout_width="fill_parent" android:layout_height="0dp"
        android:background="@drawable/bar" android:layout_weight="1">



        <RelativeLayout android:id="@+id/relativeLayout1"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:layout_gravity="center">
            <Button
                android:id="@+id/Button_HeaderFooterSubscribe"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="2dp"
                android:layout_centerVertical="true"
                android:background="@drawable/subscribe"
                />
                <Button
                android:id="@+id/Button_logout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="2dp"
                android:layout_centerVertical="true"
                android:background="@drawable/logout"
                />
                <Button
                android:id="@+id/Button_playlist"
                android:layout_marginLeft="2dp"
                android:layout_width="wrap_content"
                android:layout_centerVertical="true"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@drawable/tempadd"
                />



</RelativeLayout>

    </LinearLayout>
    <LinearLayout android:id="@+id/lldata"
        android:layout_weight="8" android:layout_width="fill_parent"
        android:layout_height="0dp" android:background="#FFFFFF">


    </LinearLayout>

    <LinearLayout android:id="@+id/llfooter"
        android:layout_weight="1" android:layout_width="fill_parent"
        android:orientation="horizontal" android:layout_height="0dp"
        android:visibility="visible" android:background="@drawable/fbg"
        android:weightSum="5.0" android:gravity="center"
        android:layout_margin="0dp">

        <Button android:id="@+id/home" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/home" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/issue" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/issue" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/browse" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/browse" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/search" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/search" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:layout_height="wrap_content" android:id="@+id/favorite"
            android:background="@drawable/favorite" android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textColor="#FFFFFF" android:padding="10px"></Button>
    </LinearLayout>

</LinearLayout>

Then Create One HeaderFooter.java Activity

public class HeaderFooter extends Activity {
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.headerfooter);
        }
 }

Now Extend above activity to your all other activities and inflate your particular view in the middle layout of the headerfooter.xml

public class Home extends HeaderFooter 
{
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
            ViewGroup.inflate(Home.this, R.layout.home, vg);
        }
}
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • 1
    This approach worked very well, thanks! (The only problem is that the footer has a very small gap between it and the bottom of the screen) – Caleb Gomer Oct 20 '11 at 19:15
  • @CalebGomer i have updated xml use it and that problem will be solved..tell me if noy – MKJParekh Oct 21 '11 at 08:21
  • @MKJParekh could you please add a sample code on how to listen to button clicks in the header ?? and I have a problem I can't extend the activity and inflate the header nothing is shown and I get no errors, any suggestions? Thank you – Moe Aug 15 '12 at 20:21
  • In the HeaderFooter activity only , you can write the button click event and that will be extended automatically to other classes.. In above code we are inflating data part in header footer xml.. @Moe – MKJParekh Aug 16 '12 at 05:22
  • This is good technique. But I have one confusion about above approach. I do not need to add HeaderFooter in my manifest file. Anyone knows why? – Chintan Dec 26 '12 at 12:19
  • @Chintan I couldn't get you.. share confusion here http://chat.stackoverflow.com/rooms/1531/casual-chat – MKJParekh Dec 27 '12 at 04:43
2

Just create a xml as you need.

Add these XML to your other screens using tag in your other Screen XMLs.

Sample is here.

You can handle the button click as you need in each activity.

Hope this helps.

Vinay
  • 2,395
  • 3
  • 30
  • 35
  • I have worked on android before. I have working apps, I just would like their GUI to look better. I will look at those samples and see what I can do. Thanks! – Caleb Gomer Oct 20 '11 at 04:31
  • @Vinay Dont just give the link this link is broken. Please update your link. – BBdev Oct 08 '13 at 09:21
0

Design Xml layout with header,footer and body.you have to extend the main activity every time,you have to change body by inflating desired layout.

Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
0

I think the element that you're liking in those layouts is the fact that they're using a FrameLayout for the lower part of the display.

They're probably doing something like this:

<LinearLayout android:orientation="vertical
  .../>
 <head layout element>
 <FrameLayout element>
 <footer layout element>
</LinearLayout>
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • Is there a difference between using a LinearLayout footer and using a FrameLayout for the footer? – Caleb Gomer Oct 21 '11 at 06:01
  • It's not the footer that's the framelayout. It's the content pane that's the frame layout. I've edit my answer to be more specific. – Kurtis Nusbaum Oct 21 '11 at 16:10
  • I see what you mean. I changed the "body" from a LinearLayout to a FrameLayout but I didn't see a difference between the two. Are there practical advantages for using the FrameLayout in this situation? – Caleb Gomer Oct 21 '11 at 20:24
  • Not that I'm aware of. I think they just look nicer. – Kurtis Nusbaum Oct 21 '11 at 20:29