How to disable back button in ActionMode bar

I am using ActionMode to edit some content, these three buttons on the right do the job, and the back / home / up button on the left becomes redundant.

Screenshot, see that button on the left

Here's ActionMode

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.editormenu, menu);
        return true;
    }
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_cancel:
                selectedView.onCancelEditing();
                selectedView.enableEditMode(selectedView,false);
                mode.finish(); 
                return true;
            case R.id.action_remove:
                selectedView.enableEditMode(selectedView,false);
                relContainer.removeView(selectedView);
                mode.finish();
                return true;
            case R.id.action_done:
                selectedView.enableEditMode(selectedView,false);
                mode.finish();
                return true;
            default:
                return false;
        }
    }
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};

      

I tried to use this back / home / up button as a "cancel" button but it looks like the id is not android.R.id.home but R.id.home

So the question is, how do I remove or disable this button, OR How do I use it?

+3


source to share


2 answers


Just call the method dispatchKeyEvent

and take action on it: See the following example:

@Override 
public boolean dispatchKeyEvent(KeyEvent event) {
    if(mActionModeIsActive) { 
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
           // handle your back button code here 
           return true; // consumes the back key event - ActionMode is not finished 
        } 
    } 
    return super.dispatchKeyEvent(event);
} 

      



For more information:

Prevent undo by clicking the Back button

+2


source


This answer worked for me:

remove back / home button from action mode by long press on android

I added these lines to my styles.xml



 <item name="actionModeCloseDrawable">@color/colorAccent</item>
 <item name="actionModeCloseButtonStyle">@style/Widget.AppCompat.ActionMode</item>
 <item name="actionModeBackground">@color/colorAccent</item>

      

I am using @color/colorAccent

as background color for ActionBar in ActionMode.

0


source







All Articles