Try to use Window.FEATURE_CUSTOM_TITLE but got Exception:You cannot combine custom titles with other title feature..

Solution 1:

I had the same issue and I fix it deleting

<item name="android:windowNoTitle">true</item>

from my theme.xml

Solution 2:

Make you create custom style in “values” folder. Make sure you code as below.

<style name="CustomTheme" parent="android:Theme"> 

Don't modify parent parameter.

This did work for me.

Solution 3:

Instead of modifying your theme.xml you may also:

create a new XML style file my_theme.xml in values folder like this:

<style name="MyWindowTitleBackground">
    <item name="android:background">#444444</item>
</style>

<style name="MyTheme" parent="android:Theme">
    <item name="android:windowTitleBackgroundStyle">@style/MyWindowTitleBackground</item>
</style>

You may define other settings as you like in this theme.

Then just use this theme in your manifest within the activity's attributes

 android:theme="@style/MyTheme" 

Finally set your custom title as always in your activity.java:

final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
    useTitleFeature = window
        .requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);

if (useTitleFeature) {
    window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
        R.layout.custom_title);
    // Set up the custom title

    main_title = (TextView) findViewById(R.id.title_left_text);
    main_title.setText(R.string.app_name);
    main_title = (TextView) findViewById(R.id.title_right_text);
    main_title.setText(R.string.Main_titleInfo);

}

Don't forget to define the custom_title.xml file in your layout folder. For example...

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

    <TextView
        android:id="@+id/title_left_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:ellipsize="end"
        android:singleLine="true" />

    <TextView
        android:id="@+id/title_right_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#fff" />

</RelativeLayout>

Solution 4:

I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:

    final Window window = getWindow();

    boolean useTitleFeature = false;
    // If the window has a container, then we are not free
    // to request window features.
    if(window.getContainer() == null) {
        useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
    }

    setContentView(layoutId);

    if (useTitleFeature) {
        window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
    }

Solution 5:

May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.

you should add this custom title in the activity which you get the Tab