16

friends,

i have created custom title bar using following titlebar.xml file with code

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle" 
  android:text="This is my new title" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:textColor="@color/titletextcolor"
  android:layout_marginLeft="25px"
   android:paddingTop="3px" 
   /> 

and java code to display custom title bar on each activity.

@Override
    public void onCreate(Bundle savedInstanceState)
    {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


}

now i want to set textview value dynamically in each activity can any one guide me how can i achieve this?

using findviewbyid here i dont get reference of that textview to set value because main layout does not contains any textbox with such a name but mytitle.

any help would be appriciated.

UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

5 Answers5

14

This is the way to set the custom title:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);

    if ( customTitleSupported ) {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
    }

    final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
    if ( myTitleText != null ) {
        myTitleText.setText("========= NEW TITLE ==========");
        myTitleText.setBackgroundColor(Color.GREEN);
    }


}
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 3
    myTitleText is null because it is not in main layout dear. it is mytitle layout. when we use r.id.somthing it looks in layout written in setContentView(R.layout.main); in my case it is not in main it is somewhere else. hope you understood my question. – UMAR-MOBITSOLUTIONS Mar 11 '10 at 06:45
  • Test first , speak later. So, where do you think the green title come from ? – Diego Torres Milano Mar 11 '10 at 08:56
  • Hi, This is changing the title bar background color but i see some offset left on both side . It is not looking good that way. Can you please help me in solving that problem? – Shaista Naaz Apr 29 '11 at 12:22
  • final FrameLayout titleContainer = FrameLayout)myTitleText.getParent(); titleContainer.setPadding(0, 0, 0, 0); – Diego Torres Milano Apr 29 '11 at 15:51
  • of course it DOESN'T WORK, when u make custom layout and u look for TextView to setup title in it.. Did u tried dtmilano? btw if it works why u check if its't null? what happen if it is? – Ewoks Oct 11 '12 at 11:38
14

SDK 2.1+

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    setTitle("========= NEW TITLE ==========");
}
coderberry
  • 3,040
  • 3
  • 26
  • 28
7

Have you tried the setTitle() method of your Activity?

Dimitar Dimitrov
  • 16,032
  • 5
  • 53
  • 55
1

my_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/header"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:background="#d4e9a9">

    <ImageView android:src="@drawable/jetpack"
        android:layout_width="wrap_content" android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" android:id="@+id/back"
        android:layout_height="wrap_content" android:layout_alignParentTop="true" />

    <TextView android:id="@+id/title" android:layout_width="wrap_content"
        android:gravity="center_vertical" android:textSize="20px"
        android:textColor="#ffffff" android:layout_alignParentRight="true"
        android:text="New Title" android:background="#a5c639"
        android:layout_height="wrap_content" android:layout_alignParentTop="true"
        android:padding="9dip" android:layout_margin="5dip" />
</RelativeLayout>

Code:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);      
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);      

((TextView)findViewById(R.id.title)).setText("gradient shadow");

findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        ((TextView)findViewById(R.id.title)).setText("loce");
    }
});

Because custom title default is fixed you should write yourself a theme:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
pengwang
  • 19,536
  • 34
  • 119
  • 168
0

Try the following:

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);        
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
     setContentView(R.layout.main);
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
     TextView mytitletext = (TextView) findViewById(R.id.myTitle);
     mytitletext.setText("========= NEW TITLE ==========");
}

You can use the hierarchyviewer to see if your view is accessible (you will see its id in the hierarchy graph)

ccheneson
  • 49,072
  • 8
  • 63
  • 68