Search code examples
androidhref

Can't get HREF to work in Android strings.xml


I'm trying to add a link to a Twitter profile in an about box. 'Regular' links such as email address and web address are handled by

android:autoLink="email|web"

in about.xml, but for a Twitter profile page I need to use html code in my strings.xml. I've tried:

<string name="twitter">Follow us on &lt;a href=\"http://www.twitter.com/mytwitterprofile"&gt;Twitter: @mytwitterprofile&lt;/a&gt;</string>

which renders html markup on the about box.

I've also tried:

<string name="twitter">Follow us on <a href="http://www.twitter.com/mytwitterprofile">Twitter: @mytwitterprofile</a></string>

which display the text "Follow us on Twitter: @mytwitterprofile", but it is not a hyper-link.

How do I do this seemingly simple task!?

Cheers, Barry


Solution

  • The simple answer is that the TextView does not support <a> tags. AFAIK, it only supports basic formatting such as <b>, <i> and <u>. However, if you supply android:autoLink="web", the following string:

    <string name="twitter">Follow us at twitter.com/mytwitterprofile</string>
    

    Will turn twitter.com/mytwitterprofile into a proper link (when set via XML like android:text="@string/twitter"; if you want to set it from code, you'll need the Html.fromHtml method someone else posted in an answer).