Overriding the default Contextual Action Bar for text selection(in WebView) in Android


I want to override the existing default Contextual Action Bar(CAB) for text selection. I mean, I want to display my own CAB, with my own buttons, when some text is selected on the webview. I tried implementing a CAB using Android Documentation. OnLongClickListener doesn’t capture the text selection event in the web view. What is the event that captures text selection? Is it possible to hide the default CAB and display my CAB on Text selection?

childWebView.setOnLongClickListener(new OnLongClickListener() {
     // Called when the user long-clicks on someView
        public boolean onLongClick(View view) {
            if (mActionMode ! null) {
                return false;

            // Start the CAB using the ActionMode.Callback defined above
            mActionMode  startActionMode(mActionModeCallback);
            return true;


I have been able to resolve this. I was also facing this issue and could not find any solution on the web.

So, if you set up a LongClick listener, the Webview would stop showing selection at all. After delving deep into the Webview code, I found that it was calling WebView’s method startRunMode and passing an instance of SelectActionCallbackMode class.

I simply extended the Webview class and overrided the startRunMode method like this:

public ActionMode startActionMode(ActionMode.Callback callback) 
    actionModeCallback  new CustomizedSelectActionModeCallback();
    return super.startActionMode(actionModeCallback);

This forced the Webview to display my Callback instead of displaying Webview’s default one. This ensured that selection worked as smoothly as before and my CAB was displayed each time selection was made. Only caveat was that I had to write code to dismiss the CAB myself.

Tested on 4.1, 4.2 and 4.3 devices.

Hope this helps.

Answered By – Pulkit Gupta

Leave a Comment