Implement “Copy Link Text” feature in the app

Issue

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.

Solution

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.

Answered By – Manuel Allenspach

Leave a Comment