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.
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 “|” .
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.
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.
text. style. StyleSpan(Typeface. BOLD), start, end, Spannable.
Just build your String in HTML and set it:
String sourceString = "<b>" + id + "</b> " + name; mytextview.setText(Html.fromHtml(sourceString));
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);
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