android – Set title for contextual action bar-ThrowExceptions

Exception or error:

I want to set the title for the contextual action bar (like in the image below).
enter image description here

I have tried the following:

getActionBar().setTitle("Title for cab");

but title is not showing up.

How to solve:

I manage to set the title by calling setTitle method of ActionMode.

mActionMode.setTitle("Title for cab");

###

This answer worked for me

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

        mode.setTitle(yourTitlehere);
        return false; // Return false if nothing is done
    }

###

According to Android documentation:

http://developer.android.com/guide/topics/ui/menus.html#CAB

It can be done at the overriden onItemCheckedStateChanged() method, when creating the MultiChoiceModeListener object.

This would do the trick. Neat and simple:

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
                                      long id, boolean checked) {
    // Set contextual action bar title
    mode.setTitle("Your title");
}

###

I assume that you aren’t talking about a static setting of the text, and that you want to change it at runtime. If so, @AmJay’s method should work. If not, then you should be using android:title in your menu’s item creation.

Leave a Reply

Your email address will not be published. Required fields are marked *