简体   繁体   中英

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 ?

在此处输入图片说明

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?

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();
                    }
                }
            }
        });

Simply remove it manually.

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

查看层次结构

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……

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM