Horizontal menu inflater on long click for web view
In whichever activity is hosting your WebView
, override onActionModeStarted()
, manipulate the menu items, and assign listeners to each. An example:
@Override
public void onActionModeStarted(ActionMode mode) {
super.onActionModeStarted(mode);
MenuInflater menuInflater = mode.getMenuInflater();
Menu menu = mode.getMenu();
menu.clear();
menuInflater.inflate(R.menu.menu_custom, menu);
menu.findItem(R.id.custom_one).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "One!"));
menu.findItem(R.id.custom_two).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "Two!"));
menu.findItem(R.id.custom_three).setOnMenuItemClickListener(new ToastMenuItemListener(this, mode, "Three!"));
}
private static class ToastMenuItemListener implements MenuItem.OnMenuItemClickListener {
private final Context context;
private final ActionMode actionMode;
private final String text;
private ToastMenuItemListener(Context context, ActionMode actionMode, String text) {
this.context = context;
this.actionMode = actionMode;
this.text = text;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
actionMode.finish();
return true;
}
}