I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like <a href="http://hello.com">hello</a>
. I have set the text using the following code.
textView.setText(Html.fromHtml(sampletext));
And also set android:autoLink="web"
in the corresponding xml of that contains the TextView
. Now the link is showing properly with blue color and underline, but I found the its just a dead link. Nothing is happening if we try to click it. What I have to do to make the link active?
After revisiting all solutions, a summary with some explanations:
will find an URL and create a link even if android:linksClickable is not set, links are by default clickable. You don't have to keep the URL alone, even in the middle of a text it will be detected and clickable.
To set a link via the code, same principle, no need for pattern or android:autoLink in layout, the link is found automatically using Linkify:
This works for me:
If you are displaying in textview the string from strings.xml, strings containing the web link should not have word "a href=". If these words are deleted from the strings.xml file then the link will work.
Check out this approach:
try this https://saket.me/better-url-handler-textview-android/
It is very good solution and easy
There are 2 cases:
"click on http://www.hello.com"
then you just have to set the autoLink attribute in the xml so that the link is automatically detected in the text:
click on <a href="http://hello.com">hello</a>
then you have to do it by code and tell the text is html, and specify a Link movement method for the click: