Skip to content

Instantly share code, notes, and snippets.

@coreform
Last active August 29, 2015 14:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreform/36ed98f98668f2e90c6a to your computer and use it in GitHub Desktop.
Save coreform/36ed98f98668f2e90c6a to your computer and use it in GitHub Desktop.
An AppCompat (i.e. uses startSupportActionMode method) solution for modifying the Contextual Action Bar (CAB) done button image, while reinstating it so that Text Selection behaviour remains as per normal. This example will replace the normal back/up icon for a tick icon when an ActionMode is created by the app and reinstate the back/up icon whe…
Drawable mDefaultActionModeDoneDrawable; //used to cache the original doneButton drawable so it can be reinstated to provide TextSelection as per normal
void customiseActionModeDoneButton(boolean showTick) {
//updated for AppCompat, based on http://stackoverflow.com/questions/6556116/how-can-i-customize-the-action-modes-color-and-text
if(showTick) {
mHandler.post(new Runnable() {
@Override
public void run() {
int doneButtonId = R.id.action_mode_close_button;
View v = findViewById(doneButtonId);
if (v == null){
//appcompat id for doneButton not found, probably some OS customisation shennanigans
return;
}
TintImageView img = (TintImageView) v;
mDefaultActionModeDoneDrawable = img.getDrawable();
img.setImageResource(R.drawable.ic_cab_done_mtrl_alpha);
}
});
} else {
int doneButtonId = R.id.action_mode_close_button;
View v = findViewById(doneButtonId);
if (v == null){
//appcompat id for doneButton not found, probably some OS customisation shennanigans
return;
}
TintImageView img = (TintImageView) v;
img.setImageDrawable(mDefaultActionModeDoneDrawable);
}
}
final class ExampleActionMode implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
customiseActionModeDoneButton(true);
return true;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
customiseActionModeDoneButton(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment