For example: Take a look at the following string resource:
 <string name="b1b">This link will take you to google.com. More text here.</string>
Now I want this string resource to look like this in my app:
This link will take you to Google. More text here.
I can't use three textviews. This was just an example. So I can't make the entire textview a link.
(Why? What I'm doing in my app is … I have say a dozen buttons, each of them sends a string resource ID as an intent to a "Text Shower Activity" … and in that I simply have a single textview which shows different texts based on which button the user clicked. So, I'm saving on app size.
Plus every such text string resource has different number of links at different places, so it's not feasible to have a single textview just for links and somehow weave it in between.)
So, I need to make a little bit of the string resource into a link. I've tried the <a> thing with no effect.
How to do this?
Example XML resource:
</string name="mlink">
    to go to Google<![CDATA[ <a href="http://google.com">click here</a>]]> 
    and <![CDATA[<a href="http://yahoo.com">this</a>]]> moves you to yahoo!
</string>
Java:
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
yourTextView.setText(Html.fromHtml(getString(R.string.mlink)));
Note that you need to put your html link inside the CDATA tag, this is the proper way to use links in String resources.
Just to add on @Droidman's answer if anyone wants to use it without CDATA, Below will also work without CDATA but we have to escaped the characters such as "<" , using the < notation.
</string name="mlink">
to go to Google <a href=\"http://google.com\">click here</a> 
and <a href=\"http://yahoo.com\">this</a> moves you to yahoo!
</string>
Java:
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
yourTextView.setText(Html.fromHtml(getString(R.string.mlink)));
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With