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 <a href=\"http://www.twitter.com/mytwitterprofile">Twitter: @mytwitterprofile</a></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
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());
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