views:

991

answers:

2

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&lt;/string&gt;
    <string name="link2">http://some.link2.com&lt;/string&gt;
</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?

Thank you,

Martyn

A: 

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.

CommonsWare
with the sample text from #1 I've tried this: tv.setText(Html.fromHtml(getResources().getString(R.string. sampleText, getResources().getString(R.string.someLink), getResources().getString(R.string.someLink2))), TextView.BufferType.SPANNABLE); but still not getting clickable links. Am I missing something in the layout or is there anything else I'm missing?
Martyn
+1  A: 

You have to implement

setMovementMethod(LinkMovementMethod.getInstance());

on your Textview

Here is a better example:

clickable-urls-in-android-textviews

dhesse