Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a specific text on TextView BOLD

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.

like image 721
Budi Darmawan Avatar asked Jan 17 '13 02:01

Budi Darmawan


People also ask

How do I make text bold in TextView?

android:textStyle attribute is the first and one of the best way to make the text in TextView bold. just use “bold”. If you want to use bold and italic. Use pipeline symbol “|” .

How do I bold text in a string?

For example, the StyleSpan can be used like this: SpannableString string = new SpannableString("Bold and italic text"); string. setSpan(new StyleSpan(Typeface. BOLD), 0, 4, Spannable.

How do I make text bold in TextView android programmatically?

TextView Bold Text – To set text style of TextView to bold, you can assign textStyle attribute with “bold” in XML layout file or change the text style dynamically in Kotlin file using setTypeface() method.

How do I make a text on TextView bold in XML?

text. style. StyleSpan(Typeface. BOLD), start, end, Spannable.


2 Answers

Just build your String in HTML and set it:

String sourceString = "<b>" + id + "</b> " + name;  mytextview.setText(Html.fromHtml(sourceString)); 
like image 136
Raghav Sood Avatar answered Sep 20 '22 14:09

Raghav Sood


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); 
like image 23
wtsang02 Avatar answered Sep 16 '22 14:09

wtsang02