Skip to content

Instantly share code, notes, and snippets.

@tseglevskiy
Last active November 12, 2015 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tseglevskiy/87227454f9b57ea69399 to your computer and use it in GitHub Desktop.
Save tseglevskiy/87227454f9b57ea69399 to your computer and use it in GitHub Desktop.
Text Selection Demo
// floating menu on long click
callback = new AcCallback();
textView1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (actionMode != null) {
return false;
}
// old startActionMode(callback)
// == new startActionMode(callback, ActionMode.TYPE_PRIMARY);
actionMode = startActionMode(callback, ActionMode.TYPE_FLOATING);
v.setSelected(true);
return true;
}
});
// selection menu!
callback = new AcCallback();
textView2.setTextIsSelectable(true);
textView2.setCustomSelectionActionModeCallback(callback);
editText.setCustomSelectionActionModeCallback(callback);
// callback
public class AcCallback extends ActionMode.Callback2 {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.context_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_one:
Toast.makeText(MainActivity.this, "One!", Toast.LENGTH_SHORT).show();
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.action_two:
Toast.makeText(MainActivity.this, "Two!", Toast.LENGTH_SHORT).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment