I have a WebView
and some EditText
s in my android app. When I select text I have an option to share this text.
Now I want to disable this text sharing feature in my whole app.
What is the most reliable way to do this?
Asked
Active
Viewed 1,043 times
1

Solvek
- 5,158
- 5
- 41
- 64
-
https://stackoverflow.com/questions/12005852/how-can-i-disable-copy-paste-and-select-toolbar-from-a-webview-inside-androi – AskNilesh Jul 16 '18 at 10:41
1 Answers
3
For a web view there is a good method [setDisabledActionModeMenuItems][1]
but it is available for android sdk 24+ only.
I used reflection like this
private void disableSelectedTextOptions() {
// TODO Method setDisabledActionModeMenuItems should be called directly after project will be migrated to SDK 24+
if (Build.VERSION.SDK_INT < 24) return;
Method method = null;
try {
method = webSettings.getClass().getMethod("setDisabledActionModeMenuItems", int.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
}
try {
method.invoke(webSettings,
/*WebSettings.MENU_ITEM_SHARE*/ 1 |
/*WebSettings.MENU_ITEM_WEB_SEARCH*/ 2 |
/*WebSettings.MENU_ITEM_PROCESS_TEXT*/ 4);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
For and EditText I used this approach to remove "Share" (and "Translate"):
public static void removeShareOption(TextView tv){
tv.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.removeItem(/*android.R.id.shareText*/0x01020035);
menu.removeItem(/* Translate */0);
return true;
}
@Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override public void onDestroyActionMode(ActionMode mode) {
}
});
}

Solvek
- 5,158
- 5
- 41
- 64