Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle localized string contains a link in a single TextView

i have a string in strings.xml file. clicks on some part of this string redirect to a task. that some part where made based on the index of the string.

Now i am trying to translate it to french but i am getting index out of bound exception as its less then the length of English strings.

Could anyone please say, what will be the best way to handle this scenario?

String separation is one thing we can do.

but i want to handle it in one text view itself.

Code for the English String:

    SpannableString spannableString = new SpannableString(getResources().getString(R.string.desc));
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Log.v("dosomething", "dosomething");
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            Log.v("task one", "task one");
        }
    };

    spannableString.setSpan(clickableSpan, 87, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    mDesc.setText(spannableString);
    mDesc.setMovementMethod(LinkMovementMethod.getInstance());
like image 986
Vji Avatar asked Mar 08 '23 10:03

Vji


2 Answers

Then you will have to detect the language that the device is using whether it uses the french strings or the english strings then, put this in each condition defining the length for the given strings file. e.g.

 if (Locale.getDefault().getLanguage().equals("en")) {
     spannableString.setSpan(clickableSpan, 87, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 }
 else if (Locale.getDefault().getLanguage().equals("fr")) {
     spannableString.setSpan(clickableSpan, 50, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 }

just change the start and the end length depending on the string that is in the strings file.

like image 141
Bryan Avatar answered Mar 23 '23 21:03

Bryan


You can use annotations and then set the ClickableSpan on the annotated part. That way, the translations should contain that information.

like image 22
Yoel Gluschnaider Avatar answered Mar 23 '23 20:03

Yoel Gluschnaider