Monday, 16 September 2013

Capture the done button in the Action mode of Action bar

Action mode in the Action bar allow the app to set special actions only in special cases, for example, when the user has selected an item from a list.

By default, it contains a list of menu items as the normal action bar. The problem is that the done button is created the the action mode itself and by default, it cannot be captured.

There is an way to capture it, but only works when the action mode is created:
ActionMode mMode = MyActivityClass.this.startActionMode(some implementation);
int doneButtonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
View doneButton = MyActivityClass.this.findViewById(doneButtonId);
doneButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // do whatever you want 
        // in android source code it's calling mMode.finish();

StackOverflow - How to recognize whether the Done button is clicked in ActionMode.

No comments:

Post a Comment