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
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);
The easiest way is
textview1.setText(Html.fromHtml("<b>Health:</b> good"));
The mistake in your code is to use string concatenation here: "\n"+ss1+strhealth+"\n\n"
which strips out all formatting because the components are taken as normal strings.
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