4

I have an activity with new support Toolbar and navigation drawer. The Toolbar relates to the content e.g. list of items. Multiple items can be selected - then ActionMode is shown (Context Action Bar). However the system ActionMode position, size and layout order does not correspond to the Toolbar, which would be appropriate.

Question is: how can I adjust the system ActionMode to correspond to (be aligned with) my Toolbar? Or is there any other recommended alternative? See my cases below

Portrait

NavDrawer may be hidden in side (1-green) - this is OK for both Toolbar and ActionMode. Both Toolbar and its content are overlapped by the navigation drawer the drawer is opened (2-yellow). However, when ActionMode is active, it is displayed always over the NavDrawer (3-red), but I want it to look like (2-yellow), because the ActionMode is related to the hidden content.

This issue is similar to this question: How to make the Contextual ActionMode Bar overlay the appcompat-v7 Toolbar but not the navigation drawer?

ActionMode portrait

Tablet/landscape

In landscape the NavDrawer fits next to the toolbar and content (left). The ActionMode always overlays the NavDrawer and Toolbar and has full screen width (right-red). Again I would like the ActionMode to be in the same position as Toolbar is (left-yellow).

ActionMode landscape/tablet

Community
  • 1
  • 1
kotucz
  • 1,190
  • 13
  • 13
  • Maybe you have to finish ActionMode. Check out this question: http://stackoverflow.com/questions/21288278/hiding-contexual-action-bar-while-navigation-drawer-is-open – Javi Mollá Feb 18 '15 at 16:40
  • Thanks for hint, but this only seems like workaround. Not exactly what I want to achieve. It would be helpful only in the portrait case, not in tablet/lanscape scenario – kotucz Feb 19 '15 at 13:37
  • I solved it with reflection. Here's an example http://stackoverflow.com/questions/26483778/display-actionmode-over-toolbar/30662727#30662727 – Gema Sanchez Jul 16 '15 at 18:06
  • I "solved" this by implementing as Google did with Gmail and Keep: as soon as the DrawerLayout calls the onDrawerSlide() I close the ActionMode. You can re-start the ActionMode once the drawer is fully closed. – agirardello Aug 10 '15 at 22:33
  • I know this is not a proper solution, but I had the same problem and I managed it by temporary disabling the Drawer by `drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);` in actionModeCallbacks and then re-enabled the drawer after I finised my stuff with Action mode by calling `drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);` – Meet Vora Jun 19 '17 at 06:07

1 Answers1

2

I solved it by using another Toolbar (actionModeToolbar), which I can put anywhere in layout and show or hide it as appropriate.

I found some useful hints how to use Toolbar here: https://gist.github.com/gabrielemariotti/ae63392e1c70bc33af8b

Here is how I created ActionMode using Toolbar with legacy ActionMode.Callback. I have used methods similar to system ActionMode for partial compatibility. Note, that ActionMode.Callback is not required anymore, because system ActionMode is not used and code can be refactored.

class ToolbarActionMode {

    Toolbar actionModeToolbar;
    ActionMode.Callback callback;       

    void startActionMode() {
        actionModeToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                toolbarActionMode.finishActionMode();
            }
        });
        actionModeToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override public boolean onMenuItemClick(MenuItem menuItem) {
                return callback.onActionItemClicked(null, menuItem);
            }
        });
        // will create menu using inflateMenu(R.menu.menu_res)
        callback.onCreateActionMode(null, null);
        invalidate();
        actionModeToolbar.setVisibility(View.VISIBLE);
    }

    void finishActionMode() {
        callback.onDestroyActionMode(null);
        actionModeToolbar.setVisibility(View.GONE);        
    }

    void invalidate() {
        // will update title + menu
        callback.onPrepareActionMode(null, actionModeToolbar.getMenu());
    }

    void setTitle(CharSequence title) {
        actionModeToolbar.setTitle(title);
    }

    void inflateMenu(int menuRes) {
        actionModeToolbar.inflateMenu(menuRes);
    }
}
kotucz
  • 1,190
  • 13
  • 13
  • I'm looking at the same issue you described in your examples. Is this still the solution you care currently using or did you find something new ? – darken Oct 28 '15 at 18:22
  • Basically we still use custom Toolbar to emulate the action mode. The code may have changed, but the idea is still the same. – kotucz Oct 29 '15 at 09:05