I have Android WebView which displays search results. Using the contextMenu and WebView HitTestResult, I have successfully implemented a list of options like open, save, copy link url.
Now, I would like to implement copy link text feature as present in Google chrome android which should copy only link text (title). A similar(not exact) feature is present in default Android browser as "Select text" option.
I don't want the code for copying text using clipboard instead my main motto is to determine the way of retrieving the link title.
The link url can be retrieved using HitTestResult getExtra() method likewise is there any way to retrieve the link text (title) ?
I have referred How to get loaded web page title in Android WebView? but it gives title after the webpage is loaded not when the link is pressed.

- 1
- 1

- 1,603
- 6
- 28
- 54
2 Answers
Unfortunately, the link you posted is the fastest way to get the page title (you may refer to the second answer, which is probably faster).
The reason behind this is that a website needs to be loaded before you can read the page title. The advantage of onReceivedTitle()
(the second answer of your link) is that it doesn't wait until the whole page is loaded. It waits until enough is loaded to retrieve the title from the document. It also notifies you every time the page title gets change (due to JavaScript or whatever).
Edit:
What chrome does with Copy link text is to copy the text of the link like this:
<a href="linkUrl">Linktext</a>
This is very difficult to achieve via a webview, since you need access to the html-content of the webpage. There are some workarounds out there (see here).
The are two APIs (selectText and copySelection) which are pending API council approval, they would help you to do this, but they are not available at the moment.
A clear, official way to do this is not available.

- 1
- 1

- 12,467
- 14
- 54
- 76
-
Do you have any insight of how **android chrome browser** implements the **Copy link text** functionality (without loading page) as an option of context menu in the search results ? Also, **Android default browser** in 4.0 have also implemented similar functionality as an option **Select text**. I have looked into the source code and found some method **webview.selectText()** but I could not relate it much. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.0.1_r1/com/android/browser/Controller.java#Controller.SelectText – r.bhardwaj Jun 17 '14 at 05:20
-
Thanks for your effort. I have already tried using WebView **emulateShiftHeld** in my code with Android version 4.4 but of no avail. As you say **This is very difficult to achieve via a webview, since you need access to the html-content of the webpage**, does the chrome browser sends **LinkText** from the html to Android code using bridge ? – r.bhardwaj Jun 17 '14 at 11:25
-
The links you posted for the pending `selectText` and `copySelection` methods are from API level 15, i.e. 4.0.1. If you look at android.webkit.WebView in API 19, you'll see that both of those methods are no longer listed, implying they did not make it through the API council approval process. Overall, it changes nothing, just thought you might want to be aware that those methods are never coming. – Sean Beach Jul 02 '14 at 20:23
For copy the text try this:-
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setText(mPostCode); Toast.makeText(getApplicationContext(), "Your code is copied.", Toast.LENGTH_SHORT).show(); } else { android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", mPostCode); clipboard.setPrimaryClip(clip); }

- 1,725
- 2
- 19
- 36
-
What is "mPostCode" here ? Actually I have to determine the **link text** pressed by user which can be used as mPostCode. – r.bhardwaj Jun 12 '14 at 11:45
-
It is the name of textView, from where you want to copy the text, or if you want you can use here editbox also. – Amit Jayaswal Jun 12 '14 at 11:56
-
Actually I need to determine the text of link (title). Suppose, I have a link **quertytest.html** (with title **Querty**) displayed in web results. For this, I have to retrieve the title **Querty** somehow which could then be further used as mPostCode. So the main issue is how to get that **title** ? Please see the behavior of "copy link text" option in android chrome browser. I want exact replica of that. – r.bhardwaj Jun 12 '14 at 12:16