Android – how to make a specific text on TextView BOLD

androidandroid-textattributestextview

I don't know how to make a specific text on TextView become BOLD.

its like this

txtResult.setText(id+" "+name);

I want the output to be like this:

1111 neil

id and name are variables that I have retrieved the value from database, and I want to make the id to bold, but only the id so the name will not affected, I have no idea how to do this.

Best Answer

While you can use Html.fromHtml() you can use a more native approach which is SpannableStringBuilder , this post may be helful.

SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text");
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), INT_START, INT_END, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv=new TextView(context);
tv.setText(str);