I'm currently rendering HTML input in a TextView like so:
tv.setText(Html.fromHtml("<a href='test'>test</a>"));
The HTML being displayed is provided to me via an external resource, so I cannot change things around as I will, but I can, of course, do some regex tampering with the HTML, to change the href value, say, to something else.
What I want is to be able to handle a link click directly from within the app, rather than having the link open a browser window. Is this achievable at all? I'm guessing it would be possible to set the protocol of the href-value to something like "myApp://", and then register something that would let my app handle that protocol. If this is indeed the best way, I'd like to know how that is done, but I'm hoping there's an easier way to just say, "when a link is clicked in this textview, I want to raise an event that receives the href value of the link as an input parameter"
Solution
I have implemented a small class with the help of which you can handle long clicks on TextView itself and Taps on the links in the TextView.
Layout
TextViewClickMovement.java
Usage
Links
Hope this helps! You can find code here.
The best way I used and it always worked for me
its very simple add this line to your code:
Example: Suppose you have set some text in textview and you want to provide a link on a particular text expression: "Click on #facebook will take you to facebook.com"
In layout xml:
In Activity:
Also create one tag provider as:
In manifest file make as entry for provider and test activity as:
Now when you click on #facebook, it will invoke testactivtiy. And in test activity you can get the data as:
Here is a more generic solution based on @Arun answer
To use it just implement
onLinkClick
ofTextViewLinkHandler
class. For instance:Just to share an alternative solution using a library I created. With Textoo, this can be achieved like:
Or with dynamic HTML source: