2

In my app I have a webView, where I'm displaying web site, when I'm trying to click on the copy button, it doesn't copy anything. How is possible to fix that?

Here what I have tried so far, but it didn't help:

webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setMinimumFontSize(1);
        webView.getSettings().setMinimumLogicalFontSize(1);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAllowContentAccess(true);  

I'm checking Logcat and see this error, while clicking on the copy button in the website: "

Uncaught (in promise) undefined

"However, this button works okay in the browser, but doesn't work in WebView.

Lucky_girl
  • 4,543
  • 6
  • 42
  • 82
  • What are you trying to copy? the html code? – Nero Dec 27 '18 at 13:17
  • @Nero text inside the website – Lucky_girl Dec 27 '18 at 13:20
  • I've found the approach in order to tackle this... basically, you'll need to catch the event when the copy button is clicked. Once you catch that event, the text will be copied into the ClipBoard and you can extract the test from the ClipBoard. I've been trying to code an example in order to catch a button click event in the WebView but haven't had any success yet. – Nero Dec 28 '18 at 22:54
  • @Nero for click event possible to use this solution https://stackoverflow.com/questions/5116909/how-can-i-get-onclick-event-on-webview-in-android What was your approach regarding click board? – Lucky_girl Dec 29 '18 at 08:27
  • @Lucky_girl have you get the solution? – Nihthiya Althaf Nov 10 '20 at 07:01

1 Answers1

0

Based on the comment you've wrote, you've mentioned that you want to extract the text from the website. You'll need to identify which text you want exactly and locate it within the HTML code.

The following solution will allow you to extract the HTML code of the given website but you'll have to narrow it down to exactly which text you want to extract further and in which attribute it's located in i.e. class/id

webView.evaluateJavascript(
    "(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
     new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String html) {
            Log.d("HTML", html); 
            // This should log the html code within the log cat
        }
});

-Would like to credit Balaji M for the answer under html content in webview as it also contributes this question.-

~~I would like to keep my previous answer in place in case you do decide to change your approach on this question~~

As you've already located the answer to the event listener where you will be able to detect when the copy button is triggered within the webView, the only part of functionality which is missing now is the paste from ClipBoard.
Should put the following code within the onCreate method:

// Gets a handle to the clipboard service.
ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);

Now it's time to copy the data from the clipboard into our String variable or any other variable type.

// Get clip data from clipboard.
ClipData clipData = clipboardManager.getPrimaryClip();
// Get item count.
int itemCount = clipData.getItemCount();
if(itemCount > 0){
    // Get source text.
    Item item = clipData.getItemAt(0);
    String copiedData = item.getText().toString();
    //For testing purpose, display toast containig your copied data
   Toast.makeText(getActivity(), copiedData , Toast.LENGTH_LONG).show();
}
Nero
  • 1,058
  • 1
  • 9
  • 25
  • 1
    I'm not exactly sure that that's exactly what I need, there s a copy button in the website, and when I'm clicking on it in the browser it works, bur it doesn't work in the WebView, so I think, that something is breaking JavaScript from the executing correctly in the WebView. – Lucky_girl Dec 28 '18 at 15:57
  • @Lucky_girl you see, I didn't know that there was a button within the webview which will copy the text automatically to the clipboard. I'll modify my answer to accommodate the requested functionality (this piece of information should've been in the initial question). – Nero Dec 28 '18 at 18:33
  • @Lucky_girl please review the answer again for the requested functionality. – Nero Dec 29 '18 at 10:19
  • unfortunately, no. – Lucky_girl Nov 10 '20 at 09:18