Skip to content Skip to sidebar Skip to footer

How To Recognize Whether The Done Button Is Clicked In Actionmode

I use ActionMode to select items in a grid. The problem is that I cannot recognize whether exactly the Done button is clicked. The only I can is to know that ActionMode is finished

Solution 1:

Please don't do that as it's implementation specific and extremely non-standard.

You can use the onDestroyActionMode callback for when an action mode is dismissed.

Solution 2:

Here is the solution:

ActionModemMode= MyActivityClass.this.startActionMode(some implementation);
intdoneButtonId= Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
ViewdoneButton= MyActivityClass.this.findViewById(doneButtonId);
doneButton.setOnClickListener(newView.OnClickListener() {

    @OverridepublicvoidonClick(View v) {
        // do whatever you want // in android source code it's calling mMode.finish();
    }
});

Solution 3:

Here is my implementation, and it's a proper hack but it works and I can't really find an alternative to doing something specific when the ActionMode DONE is clicked. I find it really weird that you can't capture this event more elegantly.

Any suggestions to making this slightly less ugly would be greatly appreciated...

In my activity..

boolean mActionModeIsActive = false;
boolean mBackWasPressedInActionMode = false;

@OverridepublicbooleandispatchKeyEvent(KeyEvent event)
{
    mBackWasPressedInActionMode = mActionModeIsActive && event.getKeyCode() == KeyEvent.KEYCODE_BACK;
    returnsuper.dispatchKeyEvent(event);
}

@OverridepublicbooleanonCreateActionMode(ActionMode mode, Menu menu)
{
    mActionModeIsActive = true;
    returntrue;
}

@OverridepublicvoidonDestroyActionMode(ActionMode mode)
{
    mActionModeIsActive = false;

    if (!mBackWasPressedInActionMode)
        onActionModeDoneClick();

    mBackWasPressedInActionMode = false;
}

publicvoidonActionModeDoneClick();
{
    // Do something here.
}

If you are using Fragments with your Activity then some of this code will probably need to be in the Fragment, and the other bits in the Activity.

@JakeWharton (and other ActionBarSherlock users) if you see this on your travels. I'd be interested to know if the above is compatible with ABS as I have yet to integrate ABS with my current project.

Post a Comment for "How To Recognize Whether The Done Button Is Clicked In Actionmode"