Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically setting links to text in strings.xml

I'm trying to make an app with localisation built in, but I want a way that I can create a web link within the text, the URL being defined elsewhere (for ease of maintenance).

So, I have my links in res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
    <string name="link1">http://some.link.com</string>
    <string name="link2">http://some.link2.com</string>
</resources>

and my localised text in res/values-en-rGB/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
    <string name="sampleText">Sample text\nMore text and link1\nMore text and link2.</string>
</resources>

I've not tested this bit, but from the localization section of developer.android.com it says that this approach to reducing content duplication should work, although I'm not sure what folder I should put Italian, for example. Would it be in 'res/values-it-rIT/strings.xml'? Lets assume that I have various other languages too.

I'm looking for a way of taking the base localised 'sampleText' and inserting my html links in, and getting them to work when clicked on. I've tried two approaches so far:

1,

Putting some formatting in the 'sampleText' (%s):

<string name="sampleText">Sample text\nMore text and <a href="%s">link1</a>\nMore text and <a href="%s">link2</a>.</string>

and then processing the text like this:

TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(getResources().getString(R.string.sampleText, getResources().getString(R.string.link1), getResources().getString(R.string.link2)));

But this didn't work when I click on the link, even though the link text is being put in to the correct places.

2, I tried to use Linkify but the regular expression route may be difficult as I'm looking at supporting non-Latin based languages. I tried to put a custom xml tag around the link text and then do something like this:

Pattern wordMatcher = Pattern.compile("<span1>.*</span1>");
String viewURL =    "content://" + getResources().getString(R.string.someLink);
Linkify.addLinks(tv, wordMatcher , viewURL );

But this didn't work either.

So, I'd like to know if there's a way of dynamically adding multiple URLs to different sections of the same text which will link to web content?

like image 675
Martyn Avatar asked Feb 11 '10 11:02

Martyn


4 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="http://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 102
dule Avatar answered Oct 17 '22 21:10

dule


In your layout set android:autoLink to web

<TextView android:text="@string/text_with_url"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="web" />

And in strings.xml just add the URL(s).

<string name="text_with_url">http://stackoverflow.com/ FTW!</string>
like image 40
ozbek Avatar answered Oct 17 '22 22:10

ozbek


Try using Html.fromHtml() to convert the HTML into a Spannable that you put into the TextView. With what you have in #1, I would expect the TextView to show the HTML source, not rendered HTML.

like image 26
CommonsWare Avatar answered Oct 17 '22 20:10

CommonsWare


You have to implement

setMovementMethod(LinkMovementMethod.getInstance());

on your Textview

Here is a better example:

clickable-urls-in-android-textviews

like image 38
dhesse Avatar answered Oct 17 '22 22:10

dhesse