Does Android Studio make part of a string (in text form) from localization resources a clickable URL?

I want to make part of my text in a TextView clickable Url , I know I can use SpannableString to reach the clickable part using something like this

 SpannableString mySpannableString = new SpannableString("a string"); ClickableSpan myClickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { // do something } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } }; mySpannableString.setSpan(myClickableSpan, 2, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

But if I use a string from my localization resources as my SpannableString , then the length of the string can change, which means that the wrong character set can be clickable. How can i do this?

+6
source share
1 answer

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.

+2
source

Source: https://habr.com/ru/post/1014183/


All Articles