1

I am working on an android app i want to know how i can make few words in bold regular and few in italic for example i have a textview with a text HELLOO now I want to display the text like this in a text view

HELLOO

please tell me how to achieve this through styles in android??

M.Tahir Ashraf
  • 222
  • 5
  • 15

5 Answers5

3

Say, you have a TextView namely etx, use the following code:


 final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");

        final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
        final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC);Span to make text italic
        sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold 
        sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
        etx.setText(sb);

The main advantage of using this approach is that you can format text dynamically.

Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • When you use SpannableStringBuilder you have to set the text with this TextView method : setText(int resid, TextView.BufferType type) so your code must be : etx.setText(sb, TextView.BufferType.SPANNABLE); – GBouerat May 18 '12 at 14:16
  • @GBouerat I think you are talking about `setText (CharSequence text, TextView.BufferType type)` not `setText (int resid, TextView.BufferType type)`. I always get the perfect result with `setText (CharSequence text)` method. So, can you please kindly explain why this approach shouldn't be used and advantage of above methods. – Imran Rana May 18 '12 at 14:29
  • what if i dont know the length of text to be styled, but i want the whole text to be styled – Anoop Oct 21 '13 at 07:09
  • @AnoopssGolden text length can be easily obtained using length() method. – Imran Rana Oct 22 '13 at 13:12
  • Yes, that can be done. Its a large activiy, contains thousands of views. The view with this kind of styling is being inflated every time. so i want to minimize the processing. – Anoop Oct 23 '13 at 04:22
  • @AnoopssGolden then you can use style & android:textStyle or a custom textview class and use setTypeface(null, Typeface.BOLD... Have a look at this: http://stackoverflow.com/questions/3078081/setting-global-styles-for-views-in-android/3166865#3166865 – Imran Rana Oct 23 '13 at 13:19
  • @ImranRana can u pls help me lets say i have text : you are followed by a person ..... so i have to bold and red in color everytime .... person .... and as well depending on condition all texts other than person should be black and normal or black and bold ?how will i do this ? but i cant give hardcoded number like 10 -15 index numbers this is the text i m recicving from server i just have to find string if found then set in red and bold and all other text should be black and normal – Erum Sep 13 '15 at 12:23
1

<b> is deprecated. use <strong> and <em> instead of <i>.

text1.setText(Html.fromHtml("<strong>bold text</strong> normal text <em>italic text</em> "));
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Omid Omidi
  • 1,670
  • 2
  • 16
  • 23
0

If you have String in strings.xml then.

<string name="startup"><b><i>HELL</i></b><i>oo</i></string> 

You can use HTML Tags Here inside <string> </string>

Bhavin
  • 6,020
  • 4
  • 24
  • 26
0

If you want to make it bold use the b tag.If you want it to be italic use i tag.If u want to make a word bold or italic put that single word inside the tag.

 <resources> 
<string name="register"> <u><b><i>SignUp</i></b></u> </string>
</resources>
Sreedev
  • 6,563
  • 5
  • 43
  • 66
-2
String text="<html>"+"<b>"+"Hell"+"</b>"+"<i>"+"oo"+"</i>"+"</html>";
c2dm
  • 219
  • 1
  • 3