Search code examples
androidandroid-support-libraryandroid-appcompatandroid-actionbar-compatcontextual-action-bar

How to use Contextual Actionbar (CAB) with support.v7.widget.Toolbar and Listview?


I'm trying to use the CAB with a ListView:

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

listView.setMultiChoiceModeListener(new ListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

            mode.setTitle(getString(R.string.list_selector_num_items_selected, listView.getCheckedItemCount()));
            Log.i("LIST",position + " selected");
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return true;
        }
... and so on

This creates the CAB with the default ActionBar which is overlaying the Toolbar in combination with this entry in my AppTheme:

<item name="windowActionModeOverlay">true</item>

This is working BUT it look not very nice.

What i would like to achieve is similar to the current Gmail app if you long-press on an email.

Any ideas how to achieve this ?


I'm using the SupportActionBar:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
}

Solution

  • Since everything else is working. I bet it is about styling. You can always style all this.

       <style name="AppTheme" parent="Theme.AppCompat">
                <!---- other atrributes -->
                <!--- changes the action mode style; height, text size, background etc -->
                <item name="actionModeStyle">@style/MyActionMode</item>
                <!--- changes left icon of that is used to close the action mode -->
                <item name="actionModeCloseDrawable">@drawable/ic_back</item>
        </style>
    
    
    
    
    
       <style name="MyActionMode" parent="Widget.AppCompat.Base.ActionMode">
     <!--- changes background of the container  -->
        <item name="background">?attr/colorPrimary</item>
     <!--- changes the action mode background when using actionbar is splitted -->
                <item name="backgroundSplit">?attr/colorPrimary</item>
     <!--- changes height of the actionmode container view -->
                <item name="height">@dimen/toolbar_size</item>
     <!--- changes text style of the title, create your own, this is the default  -->
                <item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title</item>
    <!--- changes text style of the subtitle, create your own, this is the default  -->
                <item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle
                </item>