Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Contextual ActionMode Bar overlay the appcompat-v7 Toolbar but not the navigation drawer?

Tags:

I have an activity with an app bar and a navigation drawer. The app bar is implemented using the new Toolbar class from appcompat-v7 library version 21.+, and the navigation drawer is displayed in front of it.

The activity contains a list view with items which can be selected, so I am showing a contextual action bar (CAB). It is started by calling:

ActionBarActivity.startSupportActionMode(android.support.v7.view.ActionMode.Callback callback) 

By default, the CAB is inserted above the Toolbar instead of overlaying it; it seems that the CAB will only overlay the true ActionBar by default. As show in this answer, the CAB correctly overlays the Toolbar when this theme property is set:

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

But this also makes the CAB overlay the navigation drawer, which is incorrect according to the Material design specs. The main Android design docs still recommend hiding the CAB when the navigation drawer is opened and showing it again when closed, and this could be done to "fix" the problem, but it should not be necessary. As-written, the Material design specs don't specify that the CAB should be hidden when the nav drawer opens, and because the nav drawer should open over the CAB, hiding the CAB would in fact be a visual distraction.

Is there any way to display the navigation drawer in front of the CAB while still having it overlay the Toolbar?

like image 270
str4d Avatar asked Dec 27 '14 02:12

str4d


1 Answers

I might have found a solution - or perhaps a simple workaround.
I had the same problem even with a ListFragment, when startActionMode() was used instead of the built-in setChoiceMode(). So I looked into ListFragment code and I found that startActionMode() is not called on the activity but rather on its ListView, so I've tried using a view. In my code now it apparently works as expected using either the fragment ListView:

ActionMode mActionMode = getListView().startActionMode(this); 

or using the Activity container that contains my fragment:

View aView = getActivity().findViewById(R.id.container); ActionMode mActionMode = aView.startActionMode(this); 

Also:

  • now the back button destroys the action mode whilst before it didn't
  • the CAB now properly covers the Action Bar, whilst using windowActionModeOverlay in my style was covering only part of it - at least in my case
  • the CAB icon is a back arrow rather than a tick - not sure what this means though

To be honest I'm not sure about the reasons behind this, so I'm not sure how safe this solutions is, however for the time being seems to work fine.
Should anyone have a better understanding please feel free to comment or edit.

like image 69
Guido Avatar answered Jan 19 '23 10:01

Guido