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.
by using linkify: Linkify take a piece of text and a regular expression and turns all of the regex matches in the text into clickable links
Don't forget to
This is how I solved clickable and Visible links in a TextView (by code)
Just wasted so much time to figure out you have to use getText(R.string.whatever) instead of getString(R.string.whatever)...
Anyways, here is how I got mine working. With multiple hyperlinks in the same text view too.
string example
If using XML based TextView, for your requirement you need to do just two things:
Identify your link in the string, such as "this is my WebPage." You can add it in xml or in the code.
In the xml that has the TextView, add these:
Autolink phone does not worked for me. The following worked like a charm,
After spending some time with this, I have found that:
android:autoLink="web"
works if you have full links in your HTML. The following will be highlighted in blue and clickable:view.setMovementMethod(LinkMovementMethod.getInstance());
will work with the following (will be highlighted and clickable):Note that the third option has a hyperlink, but the description of the link (the part between the tags) itself is not a link.
android:autoLink="web"
does NOT work with such links.android:autoLink="web"
if set in XML will overrideview.setMovementMethod(LinkMovementMethod.getInstance());
(i.e.; links of the third kind will be highlighted, but not clickable).The moral of the story is use
view.setMovementMethod(LinkMovementMethod.getInstance());
in your code and make sure you don't haveandroid:autoLink="web"
in your XML layout if you want all links to be clickable.