0

I am creating an app that has headers at the top of every activity and so far the way I have been doing it:

Creating a header.xml for every page. But I think there must be a more efficient way.
Can I have a constant header.xml and then change the value of the text in my onCreate-method.

This is what I have been doing so far:

chooseActivity,

with the layout chooseact.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

   <include layout="@layout/selectheader" />
   <include layout="@layout/redcell" />

   <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   />


</TableLayout>

inside that a header.xml

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



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:src="@drawable/headerrect" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="@string/leagues"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"/>


</RelativeLayout>
CharlesB
  • 86,532
  • 28
  • 194
  • 218
iamlukeyb
  • 6,487
  • 12
  • 29
  • 40

2 Answers2

1

Give the TextView in the header.xml an id, like you do for the ImageView. And then in the onCreate() method of each Activity you can set the text.

TextView headerText = (TextView) findById(R.id.header_text);
headerText.setText("The text that should show up");

header.xml with TextView using an id attribute:

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



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:src="@drawable/headerrect" />

    <TextView
        android:id="@+id/header_text"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="@string/leagues"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"/>

</RelativeLayout>
Flo
  • 27,355
  • 15
  • 87
  • 125
0

This answer can be the another approach to use..and you will not need to add header in all xml..and wont require to make header textview in each activity.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98