Reputation: 60516
How would I go about setting a SpannableString
with a string defined in strings.xml
?
For example, I have: %s liked your post
Then I do
SpannableString usernameSS = new SpannableString(username);
usernameSS.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, username.length(), 0);
getString(R.string.like_post, usernameSS);
But it doesn't work - the style doesn't get applied, I know I could use HTML's <b><i>
but is there a way to do this without having to resort to HTML? As I may need to insert a more complex styling later via TextAppearanceSpan
Upvotes: 3
Views: 2740
Reputation: 157437
yep, here the order matter:
String finalString = getString(R.string.like_post, username);
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(finalString);
int startIndex = finalString.indexOf(username);
spannableStringBuilder.setSpannable(new StyleSpan(Typeface.BOLD_ITALIC), startIndex, startIndex + username.length(), 0);
should do it. Please check for the correct start/end index
Upvotes: 4