Android – change custom title view at run time


I am using a custom title view in my application for each activity. In one of the activities, based on button clicks I need to change the custom title view. Now this works fine every time when I make a call to setFeatureInt.

But if I try to update any items in the custom title (say change the text of a button or a text view on the title), the update does not take place.

Debugging through the code shows that the text view and button instances are not null and I can also see the custom title bar. But the text on the text view or the button is not updated. Has anyone else faced this problem?
How do I resolve it?



Here's what I tried. Does not get updated even on calling postInvalidate.

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

    TextView databar = (TextView) findViewById(;
    databar.setText("Some Text");

    Button leftButton = (Button) findViewById(;
    leftButton.setText("Left Btn");

    Button rightBtn = (Button) findViewById(;

Best Solution

The problem is that the only Window implementation (PhoneWindow) uses a LayoutInflater in its setFeatureInt method and instantiates the new layout with inflate and attachToRoot=true. Consequently, when you call setFeatureInt, the new layouts are not replaced but attached to the internal title container and thus drawn on top of each other.

You can workaround this by using the following helper method instead of setFeatureInt. The helper simply removes all views from the internal title container before the new custom title feature is set:

private void setCustomTitleFeatureInt(int value) {
    try {
        // retrieve value for
        int titleContainerId = (Integer) Class.forName(

        // remove all views from titleContainer
        ((ViewGroup) getWindow().findViewById(titleContainerId)).removeAllViews();

        // add new custom title view 
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, value);

    } catch(Exception ex) {
        // whatever you want to do here..

I'm not sure whether the current setFeatureInt behaviour is intended, but it is certainly not documented one way or the other which is why I'll take this to the android devs ;)


As pointed out in the comments, the aforementioned workaround is not ideal. Instead of relying on the constant you could simply hide the old custom title whenever you set a new one.

Let's assume you have two custom title layouts:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/custom_title_1" ...


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/custom_title_2" ...

and you want to replace custom_title_1 with custom_title_2, you could hide former and use setFeatureInt to add the latter:

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