I'm fetching the data from Contacts and displaying it in the textview. I want one part t be in Bold.How to do that. I tried doing this: sb.append(Html.fromHtml("\n"+" Contact Name:" + name); But it's not working..Can anyone please help me
5 Answers
in your strings file
<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content content content content content content content</p></p>
]]>
</string>
Then in your activity
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Html.fromHtml(getString(R.string.your_text)));
-----------------OR-----------------------
Just build your String in HTML and set it:
String sourceString = "<b>" + id + "</b> " + name;
mytextview.setText(Html.fromHtml(sourceString));

- 1,234
- 14
- 30
Try the following way-
sb.setText(Html.fromHtml("<b>" +" Contact Name:" + name + "</b>"));
Reply back if this doesn't resolve the issue.
UPDATE:
If you only want to append the specified text to the TextView's display buffer then still your code is incorrect may be a typo.
You used this-
sb.append(Html.fromHtml("\n"+" Contact Name:" + name);
Instead use this -
sb.append(Html.fromHtml("<b>"+" Contact Name:" + name + "</b>"));
Note the <b> tag
start and </b> tag
end.
Also note that you haven't closed your end bracket with )
UPDATE:
I guess that sb is your StringBuffer
and not the TextView
. So you have to set this on a TextView
and not in a StringBuffer
.
Let your TextView is,
TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
Then you simply do the following ---
tv.setText(Html.fromHtml("<b> Contact Name:</b>" + name));
UPDATE:
Since you are using StringBuffer to store all the contact details so just use the following-
//the below in a loop to add each name to string buffer
for(<loop through name length>)
sb.append("<br>" + "<b>Contact Name</b>:" + name);
//finally use this outside loop to set text to the textview.
tv.setText(Html.fromHtml(sb.toString()));
NOTE: The last UPDATE is only useful if the first line is in a loop appending each name in a string buffer. Otherwise you can directly use the second last UPDATE of this answer.

- 23,126
- 28
- 107
- 185
-
I tried this technique..But it doesn't becomes bold..And i want only the left part to be bold. – sangeetha Apr 21 '14 at 07:25
-
@sangeetha - Ok check my updated answer. This will make the left part to be bold. – sjain Apr 21 '14 at 07:46
-
1@sangeetha - there is no need to append details just for this purpose as you have one dynamic field `name`. You can directly set this with `tv.setText(Html.fromHtml(" Contact Name:" + name));` while fetching data. – sjain Apr 21 '14 at 08:44
try using spanable text
StringBuilder sb = new StringBuilder();
String s1 = "Contact Name:" ;
sb.append(s1);
String nameString = " name";
sb.append(nameString);
String s2 = "\nEmail :";
sb.append(s2);
String emailString = " email";
sb.append(emailString);
String s3 = "\nPhoneNo :";
sb.append(s3);
String phNoString = " 121564";
sb.append(phNoString);
SpannableString ss1= new SpannableString(sb);
ss1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,s1.length(), 0);
ss1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), s1.length() + nameString.length(),s1.length() + nameString.length()+s2.length(), 0);
ss1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),s1.length() + nameString.length()+s2.length()+emailString.length(), s1.length()+nameString.length()+s2.length()+emailString.length()+s3.length(), 0);
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1);

- 1,530
- 2
- 12
- 23
-
In my code. I'm retrieving the contact details and appending it to the string builder. But I want only certain part to be bold. So if i use spannableString, the no need to use StringBuffer? – sangeetha Apr 21 '14 at 07:28
-
1you can use `StringBuffer` with `SpannableString`. as you can see in `s2` String I am appending `s` and `name` so you can use ` StringBuffer` instead of `s2 String`. – maddy d Apr 21 '14 at 07:37
-
According to what u have said, the code looks like this: s1="\n Contact Name:"; sb.append( s1+ name); But i have 2 more details to append(phone no & email id). So Howw to to do that. I mean if i used this: SpannableString ss1= new SpannableString(s2); -->at the end, it will make only Contact Name Bold. How to make Phone Number & Email also to look bold? – sangeetha Apr 21 '14 at 08:36
-
1see in line `ss1.setSpan(new RelativeSizeSpan(2f), 0,s1.length(), 0);` second parameter is starting index and next parameter is ending index of substring which you want to `bold`. you can use this line multiple times to achieve your desired string. – maddy d Apr 21 '14 at 08:48
-
-
protected void onPostExecute(Void result) { SpannableString ss1= new SpannableString(sb); ss1.setSpan(new RelativeSizeSpan(2f), 0,s1.length(), 0); ss1.setSpan(new RelativeSizeSpan(2f), 0,s2.length(), 0); ss1.setSpan(new RelativeSizeSpan(2f), 0,s3.length(), 0); //Set the text to the text view txtv.setText(ss1.toString()); } – sangeetha Apr 21 '14 at 10:32
-
-
And inside my method i wrote this: s1="\n Contact Name:"; sb.append(s1 + name); s2 for Phone number & s3 for email – sangeetha Apr 21 '14 at 10:35
-
-
-
I tried this and it worked for me: StringBuffer sb=new StringBuffer(); s1="\n Contact Name:"; sb.append(s1+name+"\n"); s2="\n Phone number:"; sb.append( s2+phone+"\n"); s3="\n Email:"; sb.append(s3+emailContact); sb.append( "\n") txtv.setText(Html.fromHtml(sb.toString())); but problem is..its displaying in the same line,. I tried appending new line..but still its coming continuously..What might be the problem? – sangeetha Apr 22 '14 at 05:24
-
you have not tried my code, I tried it and it before updating. Just try it. – maddy d Apr 22 '14 at 06:09
-
I tried it...Even ur code works,..but it became bold in the 2nd part also(not all,few..). I think that is bcoz of the index which I gave. I'll try giving correct index and update you soon – sangeetha Apr 22 '14 at 09:26
1.
SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
etx.setText(sb);
2.
etx.setText(Html.fromHtml("HELLOO <b>ADVENTURE!</b>"));
try above code.

- 37,851
- 12
- 116
- 113
-
Since i used to Stringbuffer to append text.Now to use SpannableStringBuilder,i need to know the index. So how to know the index of the text dynamically. Because i have appended name,phone number and email id. – sangeetha Apr 21 '14 at 10:26
you need to format your string in html form during creation then apply formHtml method on whole string. Html.formHtml() method Returns displayable styled text(Spanned) from the provided HTML string. if you are applying this method on substring(with html tags) after that appending them to StringBuffer, sb.append(Html.fromHtml("\n"+" < b>Contact Name< /b>:" + name) actually calls Spanned.toString() method resulting in removal of html tags hence you are viewing plain text in text view.
try this way this will solve your problem
StringBuffer sb=new StringBuffer();
sb.append("hello");
sb.append("<b>How</b>");
sb.append("are you");
tv.setText(Html.fromHtml(sb.toString()));

- 954
- 3
- 13
- 28
-
Thank you, now it worked..but problem is..its displaying in the same line,. I tried appending new line..but still its coming continuously..What might be the problem? – sangeetha Apr 22 '14 at 05:23
-
I tried this and it worked for me: StringBuffer sb=new StringBuffer(); s1="\n Contact Name:"; sb.append(s1+name+"\n"); s2="\n Phone number:"; sb.append( s2+phone+"\n"); s3="\n Email:"; sb.append(s3+emailContact); sb.append( "\n") txtv.setText(Html.fromHtml(sb.toString())); but problem is..its displaying in the same line,. I tried appending new line..but still its coming continuously..What might be the problem? – sangeetha Apr 22 '14 at 05:28
-
Hi thank you..It worked now..I used
instead of "\n" and now it working exactly how I expected – sangeetha Apr 22 '14 at 06:03 -
Hi I'm creating a simple student application form. I have given EditText for address as multiline. The problem I'm facing is that EditText gets overlapped with my button. Is there any way to automatically change the button position as i press enter in the Edit Text? – sangeetha Apr 23 '14 at 06:11
Title
Description here
"));` and here is the list of supported html tags http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html – Shruti Apr 14 '14 at 12:36