Android: Bold Button Text

30,668

Solution 1

Simply put your string in strings.xml and change it like this,

 <string name="hello"><b>Hello</b> World, fh!</string>

and set this text to your button like this

<Button
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAllCaps="false"
    android:text="@string/hello"
    />

Sometimes the above approach will not be helpful when you might have to use Dynamic Text. So at that case SpannableString comes into action.

  String tempString="Copyright";
  Button button=(Button)findViewById(R.id.button);
  SpannableString spanString = new SpannableString(tempString);
  spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
  button.setText(spanString);

Solution 2

we have a more better choice like this :android:textStyle="bold" android api support bold

Solution 3

You can set it using Html.fromHtml() and give as a string, a string resource with HTML elements. Hope this helps!

Solution 4

Using spans:

SpannableStringBuilder builder = new SpannableStringBuilder("Type: your type here!");
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
builder.setSpan(boldStyle, 0, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
((Button) findViewById(R.id.button)).setText(builder);

Solution 5

You can use basic markup directory in strings, e.g.

"<b>Type</b>: Location"

See Styling with HTML markup

Share:
30,668
Jay Mayu
Author by

Jay Mayu

I'm iOS / React Native Developer but I also do some Android, HTML5, Laravel and NodeJS. You can follow me at Twitter @mayooresan or connect with me at LinkedIn If you like my answers and posts then probably you will like my blog Mayu's IT Diary

Updated on July 21, 2021

Comments

  • Jay Mayu
    Jay Mayu almost 3 years

    I have a button in my application. the text in the button goes as "Type: Location" something like that.

    I'm wondering whether its possible to change the text on the button as "Type: Location"

    i.e Bold the text partially on the button??

    Thanks for yoru time in advance.