I wish to disable the native contextual menu that is shown when you select some text, the one with the select all, copy, share and search buttons. I do not however want to disable selections themselves. Ideally I would wish to extend the menu actually, but honestly, I am more than perfectly fine with just disabling it. With textfields and the like it tends to be relatively simple from the documentation I found, but I just can't figure out a way to make this work with XWalkView
/CordovaWebView
. Might be that I am just searching in entirely the wrong corner though.
Asked
Active
Viewed 1,175 times
5

David Mulder
- 26,123
- 9
- 51
- 114
-
So... Anybody care to explain why this got a downvote? – David Mulder Nov 08 '14 at 18:41
-
Were you ever able to solve this? – Chris Sprague Apr 13 '15 at 15:16
-
@Kufuma Sadly no, even put a bounty of 50 on it, but it got ignored :( – David Mulder Apr 13 '15 at 20:08
-
That's too bad. I'm trying to find a solution now. Interested in posting a 50pt bounty if I find a workaround? – Chris Sprague Apr 13 '15 at 20:10
-
@Kufuma Always looking forward to an answer! (Though posting a 50 rep bounty on it is literally impossible now :P ) – David Mulder Apr 13 '15 at 20:15
4 Answers
1
I have a workaround.
For WebView
there is a solution, but it doesn't work for XWalkView
:
WebView selection menu workaround
My gradle includes compile 'org.xwalk:xwalk_core_library:14.43.343.17'
My solution, the main idea in the onAttachedToWindow
method:
public class XWalkWebView extends XWalkView {
public XWalkWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private ActionMode.Callback mOriginalCallback;
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
try {
View innerChild = ((ViewGroup) getChildAt(0)).getChildAt(0);
Field contentViewField = innerChild.getClass().getDeclaredField("mContentView");
contentViewField.setAccessible(true);
XWalkContentView xWalkContentView = (XWalkContentView) contentViewField.get(innerChild);
Field contentViewCoreField = xWalkContentView.getClass().getSuperclass().getDeclaredField("mContentViewCore");
contentViewCoreField.setAccessible(true);
ContentViewCore viewCore = (ContentViewCore) contentViewCoreField.get(xWalkContentView);
viewCore.setContainerView(this);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
mOriginalCallback = callback;
ActionMode.Callback c = new // your callback...
return super.startActionMode(c);
}
}
0
I try Warabei's solution but it not work on 15.44.384.13. I improve to find ContentViewCore cross versions:
public class XWalkWebView extends XWalkView {
...
private Field getFields(Class clazz){
for(Field field:clazz.getDeclaredFields()){
if(ContentViewCore.class == field.getType()){
return field;
}
}
clazz = clazz.getSuperclass();
if(clazz!=null && clazz!=Object.class){
Field field = getFields(clazz);
if(field!=null)return field;
}
return null;
}
private void inject(View view){
Field field = getFields(view.getClass());
if(field!=null){
field.setAccessible(true);
try {
ContentViewCore viewCore = (ContentViewCore) field.get(view);
viewCore.setContainerView(this);
return;
}catch(Exception e){
}
}
if(view instanceof ViewGroup){
ViewGroup viewGroup = (ViewGroup)view;
int count = viewGroup.getChildCount();
for(int i = 0;i<count;i++){
inject(viewGroup.getChildAt(i));
}
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
inject(this);
}
...
To disable contextual selection menu:
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
return new ActionMode() {
@Override
public void setTitle(CharSequence charSequence) {
}
@Override
public void setTitle(int i) {
}
@Override
public void setSubtitle(CharSequence charSequence) {
}
@Override
public void setSubtitle(int i) {
}
@Override
public void setCustomView(View view) {
}
@Override
public void invalidate() {
}
@Override
public void finish() {
}
@Override
public Menu getMenu() {
return null;
}
@Override
public CharSequence getTitle() {
return null;
}
@Override
public CharSequence getSubtitle() {
return null;
}
@Override
public View getCustomView() {
return null;
}
@Override
public MenuInflater getMenuInflater() {
return null;
}
};
}

Dinh Nguyen
- 21
- 3
0
It is an old post but I haven't been able to find another solution.
A simple workaround to disable context options in crosswalk view..
- Go to your crosswalk project into res/menu/select_action_menu.xml
- Delete or comment on the item you don't want to show
- Save, build and run

Eduardo
- 1
-2
This CSS should prevent context menus in both Android and IOS, as given in the cordova template
* {
-webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
}
body {
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
}

cforcloud
- 589
- 2
- 8