Ahmed Mohamed Ahmed Mohamed - 5 months ago 8
Java Question

Problems with creating my First android application

I am new to Android programming. but i have a good knowledge of Java.

I watched mybringback android series created by travis. and thanks to him i learned too much stuff. but my problem is i can't put things together.

Anyway to get to the point, I want to create an application which all its activities contains :



  1. Header menu which is the same at all the activities.

  2. Body Container

  3. Main menu which is placed at the footer of the application which
    is also the same at all the activites. This is a screenshot i
    created to help you explain what i really mean.



I have created a little screenshot to help you understand what i really mean
Link Here

I searched a little about this, but i don't know what is the best solution.

Should i create the header menu in an xml , the footer menu in an xml file then include them in the activity using Java ?!

Is using the action bar is the answer to my solution ?

I am kinda lost .

Thank You for your time.

Answer

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.

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

        <LinearLayout
            android:id="@+id/llheader"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            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_centerVertical="true"
                    android:layout_marginRight="2dp" />

                <Button
                    android:id="@+id/Button_logout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="2dp" />

                <Button
                    android:id="@+id/Button_playlist"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="2dp" />
            </RelativeLayout>
        </LinearLayout>

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

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

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

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

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

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

            <Button
                android:id="@+id/favorite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10px"
                android:textColor="#FFFFFF" >
            </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);
        }
}