Horizontal Menu Inflater On Long Click For Web View
I am having a problem with the webview selection on longClick. I already had an implementation of a customized menu that launches on longClick. But the default menu is launching as
Solution 1:
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;
}
}
Solution 2:
This is the solution that worked for me, hope it may be useful for someone.
The only difference from the previous answer is that I created the menu dynamically, cause the static menu for some reason was not responding to menuItemClicked() function.
@Override
public void onActionModeStarted(ActionMode mode) {
System.out.println("onActionModeStarted");
if (mActionMode == null)
{
mActionMode = mode;
//mode.setTitle("Dictionary");
Menu menu = mode.getMenu();
menu.clear();
mode.getMenuInflater().inflate(R.menu.menu, menu);
List<MenuItem> menuItems = new ArrayList<>();
// get custom menu item
for (int i = 0; i < menu.size(); i++) {
menuItems.add(menu.getItem(i));
}
menu.clear();
// reset menu item order
int size = menuItems.size();
for (int i = 0; i < size; i++) {
addMenuItem(menu, menuItems.get(i), i, true);
}
}
//System.out.println("onActionModeStarted");
super.onActionModeStarted(mode);
}
private void addMenuItem(Menu menu, MenuItem item, int order, boolean isClick){
final MenuItem menuItem = menu.add(item.getGroupId(),
item.getItemId(),
order,
item.getTitle());
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
if (isClick)
// set custom menu item click
menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
BookReader.this.menuItemClicked(item);
return true;
}
});
}
public void menuItemClicked(MenuItem item)
{
System.out.println("menuItemClicked");
switch (item.getItemId())
{
case R.id.dict_menu:
testWV.searchInDict();
if (mActionMode!=null){
mActionMode.finish();
}
break;
case R.id.hi_menu:
testWV.highlightWord();
break;
case R.id.q_menu:
testWV.copyQuote();
if (mActionMode!=null){
mActionMode.finish();
}
break;
}
}
Post a Comment for "Horizontal Menu Inflater On Long Click For Web View"