4

I am developing web browser using webview. (Android API level 21)

My goal is when user copy certain text in webView, getting that text value and change it.

I used clipboard change event listner, so when event listener capture copy event, getPrimaryClip, change clip data and setPrimaryClip are processing.

But, event listener is still alive so after setPrimaryClip is called, this process is called again and again...

How can I control just one time?

Or Could you suggest other event listener?


Edit I referenced this -> made custom contextual action bar.

And with JavaScript code taught by DevTest(answer for this question), I success to make my goal.

Community
  • 1
  • 1
Jehyun Shim
  • 91
  • 2
  • 11
  • Possible duplicate of [Android WebView Javascript getSelection](http://stackoverflow.com/questions/4892111/android-webview-javascript-getselection) – Arun Shankar Mar 21 '16 at 05:30

1 Answers1

2

The only way to get text selection from a WebView is based on javascript. This is not specific to the action mode, this is how WebView text selection is supposed to be retrieved according to WebView developers' point of view. They deliberately decided to not provide an API to access text selection from Java.

The solution comprise 2 approaches.

With Android API >= 19 you can use evaluateJavascript:

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

On older builds your only resort is a custom javascript interface with a single method accepting String, which you should call via webview.loadUrl passing the same thing:

webview.loadUrl("javascript:js.callback(window.getSelection().toString())");

where js is the attached javascript interface:

webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new WebAppInterface(), "js");

and

public class WebAppInterface
{
    @JavascriptInterface
    public void callback(String value)
    {
        Log.v(TAG, "SELECTION:" + value);
    }
}

Answer from Here

UPDATE:

For modifying and copy data (as your comment), Store the selected value in a member variable like "Seleted_Value" and when on clicking copy button (which is handling action mode - search about custom action mode) modify the "Selected_Value" variable and copy to clipboard using :

ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text",Selected_Value); 
clipboard.setPrimaryClip(clip);

Details here :

Community
  • 1
  • 1
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
  • Thanks! but I have to change string value when user presses COPY button (context menu with long click) How can I handle "value" and apply to clipboard? – Jehyun Shim Mar 21 '16 at 06:21
  • @JehyunShim I updated the answer. If my answer helped, please Vote Up/Accept my answer :-) – Arun Shankar Mar 21 '16 at 06:29
  • EditText has setCustomSelectionActionModeCallback method so it is very easy to override copy action called. But in WebView, doesn't have this override method. So I think I have to make custom Webview class to implement what I want. Any way Thank you very much to help me. I will vote up your answer! – Jehyun Shim Mar 21 '16 at 07:34
  • You have to create a custom Webview method. I actually done one. Put a new question, I can give you the code. – Arun Shankar Mar 21 '16 at 09:10
  • No. It just has the part of copying simple text to clipboard. You can read about paste in the second link I gave in the answer – Arun Shankar Mar 21 '16 at 11:10
  • I tried, this link -> http://developer.android.com/intl/ko/guide/topics/text/copy-paste.html#PastePlainText . But text is not pasted even after call return; – Jehyun Shim Mar 21 '16 at 11:31
  • Paste your code, or ask a new question which will give more answers. – Arun Shankar Mar 21 '16 at 11:32
  • I made new question -> http://stackoverflow.com/questions/36130354/android-fail-to-call-default-paste-method – Jehyun Shim Mar 21 '16 at 11:55