I have two String resources as such:
<string name="give_us_feedback">Give us feedback at %1$s if you want to make the app even better!</string>
<string name="email">[email protected]</string>
I'd like to style the email part to be blue and underlined to indicate that the user can click on it (the whole TextView, not just the email text). I know to use SpannableString to color text, but it doesn't seem to work when I'm combining two strings via getString(int resId, Object... formatArgs), presumably because getString() will perform a cast or a .toString() on the Object being sent. Here's what doesn't work:
TextView emailTV = new TextView(this);
SpannableString email = new SpannableString(getString(R.string.email));
email.setSpan(new UnderlineSpan(), 0, email.length() - 1, 0);
email.setSpan(new ForegroundColorSpan(Color.BLUE), 0, email.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
String feedback = getString(R.string.give_us_feedback, email);
emailTV.setText(feedback);
Any ideas?
It's a bit tricky. Converting back to charsequence (String feedback = getString(R.string.give_us_feedback, email);
) makes disappear the Spannable
. Try this way (you want to check for the correct indexes in your string)
String emailString = getString(R.string.email);
String feedback = getString(R.string.give_us_feedback, emailString);
SpannableString email = new SpannableString(feedback);
int startIndex = feedBack.indexOf(emailString);
int endIndex = startIndex + emailString.length();
email.setSpan(new UnderlineSpan(), startIndex, endIndex, 0);
email.setSpan(new ForegroundColorSpan(Color.BLUE), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
emailTV.setText(email);
Above answers don't work if your string already contains the same text as the argument.
So this is what I do (in Kotlin). I get the start index of the argument by searching for the literal argument text in the string resource. This is with using a regex.
val text = getString(R.string.id)
val textWithArgs = getString(R.string.id, argument)
// Searches for the start index of %1$s
val startIndex = """%1${"\\$"}s""".toRegex().find(text)?.range?.start
val endIndex = startIndex?.plus(argument.length)
val styledText = if (startIndex == null || endIndex == null) {
textWithArgs
} else {
SpannableString(textWithArgs).apply {
setSpan(
ForegroundColorSpan(
ContextCompat.getColor(
context,
R.color.id
)
), startIndex, endIndex, 0
)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With