I want to override the existing default Contextual Action Bar(CAB) for text selection. I mean, I want to display my own CAB, with my own buttons, when some text is selected on the webview. I tried implementing a CAB using Android Documentation. OnLongClickListener doesn't capture the text selection event in the web view. What is the event that captures text selection? Is it possible to hide the default CAB and display my CAB on Text selection?
childWebView.setOnLongClickListener(new OnLongClickListener() {
@Override
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// Start the CAB using the ActionMode.Callback defined above
mActionMode = startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
I have been able to resolve this. I was also facing this issue and could not find any solution on the web.
So, if you set up a LongClick listener, the Webview would stop showing selection at all. After delving deep into the Webview code, I found that it was calling WebView's method startRunMode and passing an instance of SelectActionCallbackMode class.
I simply extended the Webview class and overrided the startRunMode method like this:
public ActionMode startActionMode(ActionMode.Callback callback)
{
actionModeCallback = new CustomizedSelectActionModeCallback();
return super.startActionMode(actionModeCallback);
}
This forced the Webview to display my Callback instead of displaying Webview's default one. This ensured that selection worked as smoothly as before and my CAB was displayed each time selection was made. Only caveat was that I had to write code to dismiss the CAB myself.
Tested on 4.1, 4.2 and 4.3 devices.
Hope this helps.
The official documentation Selection | Android Developers notes that:
Selecting CAB actions
You can decide which actions and elements appear in the CAB. Use the guidelines in the Action Bar pattern to decide which items to surface at the top level and which to move to the action overflow.
Dynamically adjust CAB actions In most cases you need to adjust the actions in the CAB dynamically as the user adds more items to the selection. Actions that apply to a single selected data item don't necessarily apply to multiple selected data items of the same kind.
Unfortunately, the link included in the quote links to a section which also only holds general information without actually going into details. To make things worse, it links back to the Selection | Android Developers page.
Looking a bit further, I found this tutorial from Lars Vogel. I quote (formatting mine):
A contextual action mode activates a temporary
ActionBar
that overlays the applicationActionBar
for the duration of a particular sub-task.The contextual action mode is typically activated by selecting an item or by long clicking on it.
To implemented this, call the
startActionMode(
) method on a View or on your activity. This method gets anActionMode.Callback
object which is responsible for the lifecycle of the contextualActionBar
.You could also assign a context menu to a
View
via theregisterForContextMenu(view)
method. A context menu is also activated if the user "long presses" the view. TheonCreateContextMenu()
method is called every time a context menu is activated as the context menu is discarded after its usage. You should prefer the contextual action mode over the usage of context menus.
The most prominent reference mentions that we can create our own menu using registerForContextMenu(view)
. Rather than duplicating an answer, I found this answer by J. Pablo Fernández which contains a code example on how to apply your own CAB. Registering a new context menu should override any default menu but I am not sure. As I am unable to test this at the moment, I would be glad to hear whether this solved your problem.
来源:https://stackoverflow.com/questions/15370047/overriding-the-default-contextual-action-bar-for-text-selectionin-webview-in-a