Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 761
G.ONE Avatar asked Jun 06 '16 15:06

G.ONE


2 Answers

 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); 
like image 83
dindinii Avatar answered Oct 02 '22 23:10

dindinii


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.

like image 26
Henry Avatar answered Oct 03 '22 00:10

Henry