Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

href

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

like image 926
barry Avatar asked Dec 04 '22 08:12

barry


1 Answers

The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:

<string name="sampleText">Sample text <![CDATA[<a href="www.google.com">link1</a>]]></string>

And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:

TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
like image 82
dule Avatar answered Jan 07 '23 11:01

dule