I created a custom ActionMode.Callbackone to display a custom contextual action panel for selecting text in WebView. It works fine in Android 4.4, but in 4.1-4.3 the selection knobs are not displayed. I can still perform my custom operations on a single highlighted word, but the selection cannot be changed due to missing descriptors.
In addition, when ActionModedestroyed, the selection does not disappear from the screen. On the contrary, if the user deletes elsewhere to clear the selection, ActionModeit will not be destroyed.
I'm not even sure if clearFocus()this is a method that I should call to try to remove the selection. I commented that the line and behavior in 4.4 did not change; he still worked flawlessly.
What can be done to fix these problems?
This is my current implementation:public class CustomWebView extends WebView {
private ActionMode.Callback mActionModeCallback;
@Override
public ActionMode startActionMode(Callback callback) {
ViewParent parent = getParent();
if (parent == null) {
return null;
}
mActionModeCallback = new CustomActionModeCallback();
return parent.startActionModeForChild(this, mActionModeCallback);
}
private class CustomActionModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
loadJavascript("javascript:getSelectedTextInfo()");
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId() {
case R.id.button_1:
break;
case R.id.button_2:
break;
...
default:
break;
}
mode.finish();
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
clearFocus();
}
}
}
source
share