0

I have four screens which have slightly different titles but the same background image (white color), the same header background image or background color (gray color), the same logo image (left logo) and the same red background image (right one).

I need to use this header in all activities. Can I make a custom xml which can be added in all activities?

user944513
  • 12,247
  • 49
  • 168
  • 318

3 Answers3

1

You can write your header in another xml and then include it on the other views:

<include layout="@layout/header" android:id="@+id/id" />
dmananes
  • 104
  • 6
  • i have only logo image ...can you please tell how I make header.First I need to make header than I need to include that – user944513 Oct 22 '14 at 06:59
0

You will need to keep one xml for header: (say, header.xml)

header.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/headerlayout"
    android:layout_width="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:padding="0dip"
    android:orientation="vertical"
    android:textColor="@color/black"
    >
<!-- Header content -->

</LinearLayout>

Now, in all your activity's xml, include that xml as follows:

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/body_bkgd" >

    <include
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/header" />

    <!-- other views -->

</RelativeLayout>

Hope it helps.

Edit:

For making header.xml as you want, you need to learn how to design your layouts.

Simple would be:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/headerlayout"
    android:layout_width="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:padding="0dip"
    android:orientation="vertical"
    android:textColor="@color/black"
    >
    <ImageView
        android:id="@+id/firstimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/yourimage" />

    <Button
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alert Details" 
        android:background="#f60000"
        android:textColor="#ffffff" />

</LinearLayout>
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

You could also develop a "BaseActivity" with the header. All other Activities that should contain the header could extend from the BaseActivity.

The advantage is that if your header contains some onClick events, then you have to write it only once in the BaseActivity.

code monkey
  • 2,094
  • 3
  • 23
  • 26