10

I have a string defined in string.xml like

<string name="eventtitle">Title: %1$s </string>

which is formatted using string.format . How to define the string to get only the Titel: as Bold.

Thanks in advance for the help

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
png
  • 4,368
  • 7
  • 69
  • 118

7 Answers7

22

You can do it like,

textView.setText(Html.fromHtml("<b>Title</b>: Text"));

If you have text in dynamic way..

And to define formatings in Strings.xml you can do like,

<string name="text1">This text uses <b>bold</b> and <i>italics</i> 
by using inline tags such as <b> within the string file.</string>

see This Link

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
12

Now, it is reasonably supported by Android.

you can define the string in xml as <string name="text_to_show">Hey &lt;b>This is in bold&lt;/b>

Then in code, use this to convert to CharSequence and then use it for e.g in TextView

String text = getResources().getString(R.string.text-to_show);
CharSequence styledText = Html.fromHtml(text);
textview.setText(styledText);
Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41
9

Actually, many of the answers are obsolete. After researching by myself, what I've found out that the best answer is one by @Wahib. Here's the improved version:

Define the string resource as a:

<string name="styled_text">Hey, &lt;b>this is bold&lt;/b> text</string>

Use the resource like this:

String text = getResources().getString(R.string.styled_text);
CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
textview.setText(styledText);

Here's the result:

enter image description here

bmilovanovic
  • 149
  • 1
  • 5
5

You can use HTML markup like "<b>BOLD</b> other text ...".
See this Google resource for more information.

rekire
  • 47,260
  • 30
  • 167
  • 264
MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
2

You have to style your string in the TextView in which in is displayed. See this link

  • 2
    I know this is a bit old, but that is incorrect @bvd. Strings can be styled in the string.xml as per "You can add styling to your strings with HTML markup..." via http://developer.android.com/guide/topics/resources/string-resource.html – MikeIsrael Jan 02 '13 at 13:55
0

Now, it is reasonably supported by Android.

I have already given answer to this how to add styling within a string

Community
  • 1
  • 1
Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41
0
Typeface tfaerial=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf");
Typeface tfTradeGothicLight=Typeface.createFromAsset(getAssets(), "fonts/TradeGothic-Light.OTF");

String strt_title_desc=this.getResources().getString(R.string.eventtitle);

int upto=strt_title_desc.indexOf(":"); //of you can specify 5

 if (strt_title_desc!=null)
    {
        aboutAuthTV.setTextColor(Color.BLACK);
        aboutAuthTV.setLineSpacing(1.2f, 1.5f);
        aboutAuthTV.setTextSize(23);
    SpannableString SS = new SpannableString(strt_title_desc);
    SS. setSpan ( new StyleSpan(tfTradeGothicLight.getStyle()), 0, upto,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    SS. setSpan ( new StyleSpan(tfaerial.getStyle()), upto, strt_dialog_desc.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    yourtextView.setText(SS);
}

// this for changing the font size, style and colors

String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/>
                         <i>Then Italic</i> </font>" +
                       "<br/> <font color=\"green\" >this is simple sentence </font>" +
                       "<br/><br/><br/><br/><a>this is simple sentence</a>";
      Spanned strHtml= Html.fromHtml(str);

       TextView tv = (TextView)findViewById(R.id.textView);
       tv.setText(strHtml);
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130