Manohar
Manohar

Reputation: 23394

Disable contextual Action bar in android webview on text selection

How to disable contextual action bar in web view? I Want text selection feature to remain as it is , but i don't want the Contextual action bar to come, how can i disable it ?

enter image description here

Using this code hides the contextual action bar but it is looking bad(whole screen is shaking ) and also text selection is not retaining.

    @Override
    public void onSupportActionModeStarted(ActionMode mode) {
    super.onSupportActionModeStarted(mode);
    mode.finish();
   }

How can i disable the contextual actionbar ? Is there any other way?

Upvotes: 3

Views: 1801

Answers (2)

KnIfER
KnIfER

Reputation: 750

Simply remove it manually.

When the legacy action mode bar is triggered, the view hierachy would be:

view hierachy

So just fix the unwanted hierachy by the following code:

View p2th = Utils.getNthParent(root, 2);
if (p2th!=null && p2th.getId()==R.id.action_bar_root) {
    Utils.replaceView(Utils.getNthParent(root, 1), p2th);
}

where Utils.getNthParent returns N-th parent of the root view, and Utils.replaceView just replace the action bar root with our content view .

(tested on android 4.4 and 5 sucessfully, without flicker or crash.)

What a genius solution, so simple and so stupid……

Upvotes: 0

Gaurav Pawar
Gaurav Pawar

Reputation: 249

I know it is late to answer question but i just found it and it works fine.(I tested it on 5.0.1 and it works fine)(Following code will only clear copy , paste and select all option from menu.)

inject folliwing code in your html code which you will be loading in webview,

"<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>\n" +
 "<script>\n" +
            "$(function() {\n" +
            "  $(document.body).bind('touchstart', function(e) {\n" +
            "    var selection;\n" +
            "\n" +
            "    if (window.getSelection) {\n" +
            "      selection = window.getSelection();\n" +
            "    } else if (document.selection) {\n" +
            "      selection = document.selection.createRange();\n" +
            "    }\n" +
            "\n" +
            "    selection.toString() !== '' && ReaderJavaScript.onTextselectionEnded(true)\n" +
            "  });\n" +
            "});\n" +
            "</script>" 

see in code i used ReaderJavaScript interface which will act as bridge between js code and activity to carry information on call of ReaderJavaScript.onTextselectionEnded(true). so whenever user end selection it will indicate and at that time you just have to add following code to clear menus in your activity,

 readerView.setTextSelectionFinishedListener(new ReaderView.TextSelectionFinishedListener() {
            @Override
            public void onTextSelectionEnded(boolean textSelectionEnded) {
                if (mActionMode != null){
                    if (mActionMode.getMenu() != null){
                        mActionMode.getMenu().clear();
                    }
                }
            }
        });

Upvotes: 2

Related Questions