Selection handles do not appear in WebView (Android 4.0-4.3)

I created a custom ActionMode.Callbackone to display a custom contextual action panel for selecting text in WebView. It works fine in Android 4.4, but in 4.1-4.3 the selection knobs are not displayed. I can still perform my custom operations on a single highlighted word, but the selection cannot be changed due to missing descriptors.

In addition, when ActionModedestroyed, the selection does not disappear from the screen. On the contrary, if the user deletes elsewhere to clear the selection, ActionModeit will not be destroyed.

I'm not even sure if clearFocus()this is a method that I should call to try to remove the selection. I commented that the line and behavior in 4.4 did not change; he still worked flawlessly.

What can be done to fix these problems?


This is my current implementation:
public class CustomWebView extends WebView {

    private ActionMode.Callback mActionModeCallback;

    @Override
    public ActionMode startActionMode(Callback callback) {
        ViewParent parent = getParent();
        if (parent == null) {
            return null;
        }
        mActionModeCallback = new CustomActionModeCallback();
        return parent.startActionModeForChild(this, mActionModeCallback);
    }

    private class CustomActionModeCallback implements ActionMode.Callback {

        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.contextual_menu, menu);
            return true;
        }

        // Called each time the action mode is shown.
        // Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // This method is called when the handlebars are moved.
            loadJavascript("javascript:getSelectedTextInfo()");
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId() {
            case R.id.button_1:
                // do stuff
                break;
            case R.id.button_2:
                // do stuff
                break;

            ... // cases for other buttons

            default:
                break;
            }

            mode.finish(); // Action picked, so close the CAB
            return true;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO This does not work in 4.3 (and probably anything older).
            clearFocus(); // Remove the selection highlight and handles.

        }
    }
}
+4
source share
1 answer

I never found out what I was doing wrong that caused this behavior. Fortunately, I found a solution that displays the descriptors appropriately and allows you to perform custom actions. This solution can be found on this subject: fooobar.com/questions/193230 / ...

, ActionMode (, ..) menu.xml, MenuInflater onActionModeStarted. onClick , Activity, .

0

Source: https://habr.com/ru/post/1536987/


All Articles