0

I have to create a custom title bar, to set a dynamic text. This would be something like setting a Title, and below, the dynamic subtitle on the Actionbar.

In this case, the App title goes on the left side, as for default. The dynamic text goes in the right side, and must change dynamically.

This is the layout custom_titlebar.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="vertical" >

<TextView 
    android:id="@+id/title_left_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="left" />

<TextView 
    android:id="@+id/title_right_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="right"
    android:layout_alignParentRight="true" />    
</RelativeLayout>

In the manifest I've defined:

<application
    android:theme="@style/Theme.NoTitle" >

And on the Activity:

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

    //customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //HAVE COMENTED THIS, BECAUSE IT THROWS: You cannot combine custom titles with other title feature..

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_titlebar);

    TextView title = (TextView) findViewById(R.id.title_left_text);
    title.setText(getTitle());

To change text dynamically, I also have on the Activity:

public void setTitleStatus(String right) {
    if (right.length() > 20) {
        right = right.substring(0, 20);
    }
    TextView status = (TextView) findViewById(R.id.title_right_text);

    status.setText(right);
}

The problem is that is throwing a NPE on the line of the onCreate where I set the title (title.setText(getTitle());)

masmic
  • 3,526
  • 11
  • 52
  • 105

2 Answers2

1

change

title.setText(getTitle().toString());
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • if is this your own getTitle() method? – Piyush Nov 20 '13 at 13:50
  • I don't have getTitle() method. I supossed it was a predefined method. What would be the way to set the app title there? – masmic Nov 20 '13 at 13:57
  • UPDATE-> I've tryed setting `title.setText("TITLE")` but continues throwing NPE – masmic Nov 20 '13 at 14:00
  • Not really. Gives me the problem when defining styles. If I set in the theme: `@android.style/Theme.black` it does right. but if I set `Holo` theme then appears this on the logcat: `You cannot combine custom titles with other title feature` – masmic Nov 20 '13 at 15:00
  • The problem is getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_titlebar); here – Piyush Nov 21 '13 at 09:16
0

Look in the Android samples. In the Bluetooth project is made a custom action bar, that changes it's state in different occasions

Hitman
  • 588
  • 4
  • 10
  • Yes, that's the way I have it now. But I need it without the ActionBar – masmic Nov 20 '13 at 12:14
  • why do you need without ActionBar ? if you have a nice example that works, and your idea is to show a title in top of the activity why not use this method ? – Hitman Nov 20 '13 at 12:19