Disable contextual Action bar in android webview on text selection

霸气de小男生 提交于 2019-12-21 06:00:32

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/41549661/disable-contextual-action-bar-in-android-webview-on-text-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!