androidspannablestringspannablestringbuilder

SpannableStringBuilder setting String partially as bold not working


I'm using SpannableStringBuilder to set a string partially with a style of bold, however it's not working and I am unsure of what I am doing incorrectly.

String normalText = "New Here? ";
String boldText = "Register";
        
SpannableStringBuilder str = new SpannableStringBuilder(normalText + boldText);
tr.setSpan(new StyleSpan(Typeface.BOLD), normalText.length(), str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mBtnRegister.setText(str);

I've followed this SO example:

https://stackoverflow.com/a/41460376/11110509


Solution

  • The specified code looks to be working well as visible in the screenshot here: https://i.sstatic.net/MZX7G.jpg

    Please check your code for other mistakes causing wrong behavior.