how to have bold and normal text in same textview in android?

G.ONE picture G.ONE · Jun 6, 2016 · Viewed 16.6k times · Source

I have searched internet and tried the following code but its not working

SpannableString ss1 = new SpannableString("Health: ");
           ss1.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, ss1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textview1.setText("\n"+ss1+strhealth+"\n\n");

i want output to be like this Health: good

where strhealth = good But it is coming out Health: good What is the mistake ?

I am using android studio 2.1.1

Answer

dindinii picture dindinii · Jun 6, 2016
 String txt1="Health: ";
 SpannableString txtSpannable= new SpannableString(txt1);
 StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
 txtSpannable.setSpan(boldSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

 builder.append(txtSpannable);

 String txt2="good";
 builder.append(txt2);

 textview1.lblStatus.setText(builder, TextView.BufferType.SPANNABLE);