coder
coder

Reputation: 5390

Detecting selection changes in WebView

I was able to get the selected text using the following method:

webview.evaluateJavascript("(function(){return window.getSelection().toString()})()",
    new ValueCallback<String>()
    {
        @Override
        public void onReceiveValue(String value)
        {
            selected = value;
            Log.v(TAG, "SELECTION:" + value);
        }
    }
);

And I detect the first selection using when the motion event detected by the onTouchEvent is ACTION_UP.

webview.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if(event.getAction() == MotionEvent.ACTION_UP)
        {
           //webview started selection a word
        }
    }
});

My problem is to be able to detect when the selection changes using the handlers. Unfortunately ACTION_MOVE and ACTION_DOWN are not getting called while changing the selection using the default selection handlers.

Kindly note that when I use the ActionMode CallBack function, the default selection stops working.

Upvotes: 8

Views: 1830

Answers (1)

user1135300
user1135300

Reputation:

Instead of trying to mange the touches try letting the system do it for you:

How to get the selected text of webview in ActionMode override

@Override
public void onReceiveValue(String value)
{
    Log.v(TAG, "SELECTION:" + value);
}

with possibly.... selection change ? :)

Upvotes: 1

Related Questions