-1

i have a signin screen, in multi-language app, in this screen we displpay errors that contains some actions(create account-reset password..etc), these action must be underlined and clickable. to achieve that, i set some part of strings as underlined with <u></u> tag. and i set my text with:

HtmlCompat.fromHtml(StringUtils.getLocalizedString.get(R.string.signin_error_404), HtmlCompat.FROM_HTML_MODE_LEGACY)

but i don't know how to make this part of string clickable.

Badr At
  • 658
  • 7
  • 22

1 Answers1

-1

use ClickableSpan this will make clickable word respond to click event

TextView textView = findViewById(R.id.tv_links);
final String login = getString(R.string.login);
final String register = getString(R.string.register);
String format = getString(R.string.title);
String title = String.format(format, login, register);

addLinksClickEvent(title, textView, new Pair(login, new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, login + " Clicked", Toast.LENGTH_SHORT).show();
        }
    }), new Pair(register, new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, register + " Clicked", Toast.LENGTH_SHORT).show();
        }
    }));

the method wchich do the clicks part which is inspired from one answers of this question but in java

@SafeVarargs
public final void addLinksClickEvent(final String text, TextView textView, Pair<String, View.OnClickListener>... links) {
    final SpannableString spannableString = new SpannableString(text);
    for (final Pair<String, View.OnClickListener> link : links) {
        ClickableSpan clickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View view) {
                Selection.setSelection((Spannable) ((TextView) view).getText(), 0);
                view.invalidate();
                link.second.onClick(view);
            }
        };
        int startIndexOfLink = text.indexOf(link.first);
        spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.first.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}

I use Arabic and English languages so

values

<string name="title">You can %1$s or if you donn\'t have account you can %2$s</string>
<string name="login">login</string>
<string name="register">register</string>

values-ar

<string name="login">تسجيل الدخول </string>
<string name="register">التسجيل </string>
<string name="title">يمكنك %1$s, اذا لما يكمن لديك حساب يمكنك %2$s من هنا</string>
Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21
  • Yes i know this approach!, but i want to use it, beaause we have multiple languages in our app, and i don't want to give the start and the end index in a explicit way. – Badr At Dec 25 '19 at 13:49
  • you can get your current language words as string then use it's length instead of hard coding indexes I think it will work – Mohammed Alaa Dec 25 '19 at 15:12
  • (idont want**: correction of the first comment) -i think that in all cases, i have to store the targetted sentence in a way to get the start and the end index later. if there is a way to extract just the text which is between tag dynalically, it will be great to use it with ClickableSpan – Badr At Dec 25 '19 at 16:51