Amit Jayant
Amit Jayant

Reputation: 2621

Set title for contextual action bar

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.

Upvotes: 24

Views: 6456

Answers (4)

Amit Jayant
Amit Jayant

Reputation: 2621

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

mActionMode.setTitle("Title for cab");

Upvotes: 21

German Latorre
German Latorre

Reputation: 11128

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");
}

Upvotes: 1

Bahaa Hany
Bahaa Hany

Reputation: 754

This answer worked for me

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

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

Upvotes: 5

AllDayAmazing
AllDayAmazing

Reputation: 2383

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.

Upvotes: 0

Related Questions