0

I have create a simple custom title which is showing perfectly. However I am not sure what id to give my TextView in order for the setTitle method to work its magic.

My first choice was @android:id/title but that did not work.

Example I have defined a custom title layout in xml and added it to my activity.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_title"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:scaleType="centerInside"
        android:onClick="home"
        android:src="@drawable/logo" />

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_centerVertical="true"
        android:text="TextView" />
</RelativeLayout>

The TextView is now the "title" container. What I want is for the android:label value from the manifest to automatically be put in this new title TextView.

Ie. the default method setTitle must be adding this value to some defined view id - I just need that id.

Charles
  • 50,943
  • 13
  • 104
  • 142
slott
  • 3,266
  • 1
  • 35
  • 30

2 Answers2

2

setTitle sets the title of the activity, it has nothing to do with TextView's you need to use setText() to set the text of a TextView:

((TextView)findViewById(R.id.text_view_id)).setText("TextView text");

EDIT: Check this solution -> how to set custom title bar TextView Value dynamically in android?

Community
  • 1
  • 1
Adam Honoré
  • 183
  • 8
  • Yes I know how to set the text of a text view. What I am asking for is an id to use in my custom title layout so that the setTitle method will still work. – slott May 24 '12 at 07:18
1

I had to create a similar feature with a custom title. I highly doubt you will be able to do it by matching an id, that would be a huge security flaw if Android allowed that. What I did was extend Activity and then override the setTitle method in a fashion like...

TextView mCustomTitle;

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.myLayout);
     mCustomTitle = (TextView)findViewById(R.id.title);
}

@Override
public void setTitle(CharSequence title) {
    //you can override the other setTitle as well if you need it
    mCustomTitle.setText(title);        
}
MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
  • This is more or less what I did. I included the title from the manifest also using getTitle. Ie. .setText(getTitle()); – slott May 25 '12 at 10:31
  • Did you create it as a base class? I found that really took most of the hassle out of it? – MikeIsrael May 25 '12 at 16:26
  • Yes I have a BaseActivity class that I extend from. It makes things easier but I still think it would be nice if the setTile could work with a custom title xml if the correct id was used for the testview holding the title. – slott Jun 18 '12 at 07:01
  • @slott glad to hear it helped, if so please accept the answer. Also I am guessing that if they exposed that element it could lead to some huge security risks for malicious behaviors. – MikeIsrael Jun 18 '12 at 07:10