I have the following TextView defined:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:autoLink="web" android:id="@+id/infoTxtCredits"
android:layout_centerInParent="true"
android:linksClickable="true"></TextView>
where @string/txtCredits
is a string resource that contains <a href="some site">Link text</a>
.
Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me what I'm doing wrong? Do I have to set an onClickListener for the TextView in my activity for something as simple as this?
Looks like it has to do with the way I define my string resource. This does not work:
<string name="txtCredits"><a href="http://www.google.com">Google</a></string>
But this does:
<string name="txtCredits">www.google.com</string>
Which is a bummer because I would much rather show a text link than show the full URL.
Use this...
and add permission in manifest file
Be sure to not use
setAutoLinkMask(Linkify.ALL)
when usingsetMovementMethod(LinkMovementMethod.getInstance())
andHtml.fromHTML()
on properly formattedHTML
links (for example,<a href="http://www.google.com/">Google</a>
).I added this line to the
TextView
:android:autoLink="web"
Below is an example of usage in a layout file.
layout.xml
samplestring.xml
I hope this will help you;
The following should work for anyone who is looking for a combination of text and hyperlink within an Android app.
In
string.xml
:Now you can utilise this
string
in any givenView
like this:Now, in your Activity or Fragment, do the following:
By now, you don't require to set
android:autoLink="web"
orandroid:linksClickable="true"
using this approach.I hope you will find this helpful.
You need only this:
Insert this line to TextView, that can be clickable with reference to the web. URL address set as a text of this TextView.
Example: