All I found are two suboptimal ways. He still uses Spannable, but listen to me as I am in the specific situation you are talking about.
Firstly, there is a method that you mention, and you can go through a gigantic instruction of language operators and decide the range for each of them. This is, of course, terrible, but it is the most accurate way to ensure that the click coverage is correct.
eg.
if (Locale.getDefault().getLanguage().contentEquals("en") { // spannable gets substring a,b else if (Locale.getDefault().getLanguage().contentEquals("fr") { // spannable gets substring x,y }
Again, not good.
Now, if your translators can handle substring, IE
<string name=suba>Some intro text to</string> <string name=sublinka>linkable A</string> <string name=subb>and then some intro text to</string> <string name=sublinka>linkable B</string>
Then you can programmatically create Spannable strings, such as:
TextView textView = (TextView) findViewById(R.id.text_view); String subA = getString(R.string.suba); String subLinkA = getString(R.string.sublinka); String subB = getString(R.string.subb); String subLinkB = getString(R.string.sublinkb); SpannableStringBuilder spannableText = new SpannableStringBuilder(subA); spannableText.append(subLinkA); spannableText.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(this, "Sub Link A Clicked", Toast.LENGTH_SHORT).show(); } }, spannableText.length() - subLinkA.length(), spannableText.length(), 0); spannableText.append(subB); spannableText.append(subLinkB); spannableText.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(this, "Sub Link B Clicked", Toast.LENGTH_SHORT).show(); } }, spannableText.length() - subLinkB.length(), spannableText.length(), 0); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setText(spannableText, TextView.BufferType.SPANNABLE);
As I said, this is really the “worst” situation, but I thought that I would at least throw it away, because I don’t think there are other ways to do this with localization, since the compiler does not know how the string is translated.