Toolbar handles clickEvents while overlayed by ActionMode

238 views Asked by At

I am using Toolbar from Appcompat with ActionMode. The ActionMode overlays the Toolbar. Now, if i click on an empty Space in the ActionMode an underlying ActionButton from the Toolbar handles the ClickEvent.

Is this normal behaviour? How can i prevent this behaviour?

((ActionBarActivity) getActivity()).getSupportActionBar().startActionMode(new ActionMode.Callback() {
                            @Override
                            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                                actionMode.getMenuInflater().inflate(R.menu.actionmode, menu);
                                MenuItem menuItem = menu.findItem(R.id.number);
                                MenuItem menuItem1 = menu.findItem(R.id.action_edit);
                                TextView textView = (TextView) menuItem.getActionView();
                                ImageButton imageButton = (ImageButton) menuItem1.getActionView();
                                imageButton.setBackgroundResource(R.drawable.ic_action_edit);
                                textView.setText("0 selected");
                                // Return true so that the action mode is shown
                                return true;
                            }

                            @Override
                            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                                // As we do not need to modify the menu before displayed, we return false.
                                return false;
                            }

                            @Override
                            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
                                // Similar to menu handling in Activity.onOptionsItemSelected()
                                switch (menuItem.getItemId()) {
                                    case R.id.action_delete:
                                        // Some remove functionality
                                        return true;
                                }

                                return true;
                            }

                            @Override
                            public void onDestroyActionMode(ActionMode actionMode) {
                                // Allows you to be notified when the action mode is dismissed
                            }
                        });

XML for my ActionMode in /res/values/menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/number"
        android:title="number"
        android:actionViewClass="android.widget.TextView"/>
    <item android:id="@+id/action_edit"
        android:title="edit"
        android:actionViewClass="android.widget.ImageButton"/>
    <item android:id="@+id/action_delete"
        android:title="delete"
        android:icon="@drawable/ic_action_delete"/>
</menu>

1

There are 1 answers

0
Marcin Swierczek On

You will need to set the action mode layout to be clickable.

First set the custom Theme for entire application or a single Activity in the AndroidManifest file:

<application android:theme="@style/Theme.CustomTheme">

In themes.xml resource file set the new actionModeStyle for your theme:

<style name="Theme.CustomTheme" parent="Theme.AppCompat.NoActionBar">
   <item name="actionModeStyle">@style/action_mode_style</item>
</style>

Then your action_mode_style should have android:clickable set to true

<style name="action_mode_style" parent="@style/Widget.AppCompat.ActionMode">
    <item name="android:clickable">true</item>
</style>