Sie sind auf Seite 1von 5

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!


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.


improve this question

Budi Darmawan
353 1 4 7

Jan 17 '13 at 2:02

Krushna Chulet
797 4 8

Feb 26 '14 at 11:37

Possible duplicate of Is it possible to have multiple styles inside a TextView? Ciro Santilli Feb 8 at 13:29
add a comment

3 Answers


Order By


Just build your String in HTML and set it:

String sourceString = "<b>" + id + "</b> " + name;


improve this answer

Raghav Sood
56.8k 15 120 138

Jan 17 '13 at 2:04

it works, finally I can move forward. thanks man. Budi Darmawan Jan 17 '13 at 2:20

Graet! It's so a beautiful trick... +1 Ali Bagheri Shakib May 16 '14 at 19:21

why this doesn't work mBox..setText(Html.fromHtml("<b>" + name + "</b>") + doNotApplyHTML); any idea thanks Shan Xeeshi Aug 19 '14 at 10:05

Thanks man it works smoothely Arslan Ahmad Feb 11 '15 at 5:09

truly, an elegant trick. thanks! Mill Feb 27 '15 at 13:58

show 1 more comment


While you can use Html.fromHtml() you can use a more native approach which is SpannableStringBuilder , this post may be helful.
final SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text");
TextView tv=new TextView(context);


improve this answer

9,255 4 32 56

Jan 17 '13 at 2:09

Oct 28 '13 at 14:36

Please explain downvote. wtsang02 Apr 10 '14 at 22:19

Whoever downvoted has no idea how Android works. ;) Martn Marconcini Aug 12 '14 at 22:32

Very nice. Always much better to go with a native pure Android solution. The above HTML version may work, but it's not nearly as elegant. idratherbeintheair Sep 5 '14 at 23:59

If this answer doesn't works try to append your text with spannable string some thing like : mSpannable.append(yourremainingtext); Gowtham Kumar Mar 9 '15 at 8:11
add a comment


I thought that the chosen answer didn't provide a satisfactory result. I have written my own function which takes 2 strings; The full text and the
part of the text you want to make bold.
It returns a SpannableStringBuilder with the 'textToBold' from 'text' bolded.
I find the ability to make a substring bold without wrapping it in tags useful.

* Makes a substring of a string bold.
* @param text
Full text
* @param textToBold Text you want to make bold
* @return
String with bold substring
public static SpannableStringBuilder makeSectionOfTextBold(String text, String textToBold){
SpannableStringBuilder builder=new SpannableStringBuilder();
if(textToBold.length() > 0 && !textToBold.trim().equals("")){
//for counting start/end indexes
String testText = text.toLowerCase(Locale.US);
String testTextToBold = textToBold.toLowerCase(Locale.US);
int startingIndex = testText.indexOf(testTextToBold);
int endingIndex = startingIndex + testTextToBold.length();
//for counting start/end indexes
if(startingIndex < 0 || endingIndex <0){


improve this answer

123 1 5

Jul 15 '14 at 9:08

Derek Beattie
7,378 2 19 39

Jun 28 '15 at 16:53

Can you provide an example of how to use this in code? MicroR Oct 22 '15 at 16:19

@MicroR You need to pass two Strings, the second String needs to be a section of text contained within the first. e.g. makeSectionOfTextBold("This is some great text.", "great").
Leon Oct 30 '15 at 16:09
add a comment

Your Answer

log in



By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc