In Ionic Capacitor app, How to disable long click on WebView text selection in android

Issue

I have a hybrid app made with Svelte + capacitor. In the app we are loading digital book and we are showing custom context/tooltip menu on text selection. It is working fine on Web & Desktop apps but not on Android. On Android it is showing android default menu (copy/ paste etc). We need to disable default android text selection menu.

Tried below code

 mWebView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return true;
            }
        });
        mWebView.setLongClickable(false);

Solution

In order to hide default text selection menu, we need to override
the default behaviour of android action mode.

By adding these 2 methods in MainActivity, you can hide the default text selection menu in Android.

@Override
public void onActionModeStarted(ActionMode mode) {
    if (mActionMode == null) {
        mActionMode = mode;
        Menu menu = mode.getMenu();
        menu.clear();


    }
    super.onActionModeStarted(mode);
}



@Override
public void onActionModeFinished(ActionMode mode) {
    mActionMode = null;
    super.onActionModeFinished(mode);
}

Answered By – RajuPedda

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published