Reputation: 492
I want to remove COPY, SELECT ALL and FIND from android contextual action bar and add custom menu items.
This is appearing while selecting text on webview. I am trying to add Text Highlights on webview using js.
Upvotes: 2
Views: 1969
Reputation: 2090
In order to accomplish what you want, you will need to create an entirely new contextual action bar. This is done through creating a custom ActionMode
. Within your WebView
, create a nested class that implements ActionMode.Callback
. You can use this as a template:
public class CustomWebView extends WebView {
private ActionMode.Callback mActionModeCallback;
@Override
public ActionMode startActionMode(ActionMode mode) {
// This block is directly from the WebView source code.
ViewParent parent = getParent();
if (parent == null) {
return null;
}
mActionModeCallback = new CustomActionModeCallback();
return parent.startActionModeForChild(this, mActionModeCallback);
}
private class CustomActionModeCallback implements ActionMode.Callback {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
// Called each time the action mode is shown.
// Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// This method is called when the selection handlebars are moved.
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_button_1:
// Do stuff
break;
case R.id.menu_button_2:
// Do stuff
break;
default:
// You did not handle the action, so return false
// If you have implemented a case for every button,
// this block should never be called.
return false;
}
// If you want to close the CAB immediately after
// picking an action, call mode.finish().
// If you want the CAB to persist until the user clears the selection
// or clicks the "Done" button, simply return true.
mode.finish(); // Action picked, so close the CAB
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mode = null;
}
}
}
<!-- context_menu.xml -->
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_button_1"
android:icon="@android:drawable/menu_button_1"
android:showAsAction="always"
android:title="@string/menu_button_1">
</item>
<item
android:id="@+id/menu_button_2"
android:icon="@drawable/menu_button_2"
android:showAsAction="ifRoom"
android:title="@string/menu_button_2">
</item>
</menu>
ActionMode.java
) for those functions. Implement a new case in CustomActionModeCallback.onActionItemClicked
(where you handle your button events), copy/paste the functionality from source, and add a corresponding button in your XML file. You can even use the native icon for those functions: android:icon="@android:drawable/[name_of_desired_icon]
For reference, this information is from the Android Developers website.
http://developer.android.com/guide/topics/ui/menus.html#CAB
Upvotes: 2
Reputation: 2010
May be this helps you and also stack members...
https://github.com/btate/BTAndroidWebViewSelection
Upvotes: 0