0

I have added the code in strings.xml like

<string name="terms_agree_text">By Logging in, you are agreeing to our &lt;u>terms and condition&lt;/u> and &lt;u>privacy policy&lt;/u></string>

also I have added the same String in bhahasa language like this

 <string name="terms_agree_text">Dengan masuk, Anda menyatakan bahwa Anda telah menyetujui &lt;u>syarat dan ketentuan&lt;/u> dan &lt;u>kebijakan privasi&lt;/u> kami</string>

then I have set the label like this

mBinding.termsAndCondition.setText(Html.fromHtml(getString(R.string.terms_agree_text)));

Here I have to do the clickable event for this 2underline text. But, I tried to use the

sp.setSpan(click, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

But this string index is wrong for the other locale. So Currently I have added View to clickable in the text.

So can you please advise to do this. Thanks

CarinaMj
  • 187
  • 2
  • 14
  • Where are you getting startIndex from. Span relies on the start and end index and you have to supply it. So if you are doing a search on the string then you need to find it for the current string and confirm that you are getting the localized string on both the search span and the presentation layer. – Sam Oct 05 '17 at 02:39
  • start index is = 40 from the English but in the Bahasa is 59 is the Start index. then how is it possible to do using the Span in my case ?? – CarinaMj Oct 05 '17 at 03:33

1 Answers1

0

The span must be based on a part of the string if it is meant to be a spanned area of the string. I am assuming you are attempting to hard code the starting index of the span. If you intend to do this, then you need to have a hard coded index per language and would require you to extract it to a values-en and values-jp and values-sp and every other values you support and have a dimens with <start_index_of_terms_span>40 and in the other language it would be <start_index_of_terms_span>59 for example.

However, you would need to know the index for each of the supported languages.

Why not instead just break the string up into multiple sections. Apply the color and style to the one section or phrase that is translated for terms and conditions or privacy policy instead of trying to highlight or change color of it within a larger string. This would be a much easier fix then applying a dynamic span based on all languages.

But if you must do this, then create your dimens value folder for each language and adjust the dimens value for the starting point. Or you could just cheat and check the user's language and determine which start index to use based on a large switch case per language. Either way it will be a bit messy, so it's up to you on which messy you are fine with.

Goodluck.

Sam
  • 5,342
  • 1
  • 23
  • 39