Android: Bold Button Text
Asked Answered
C

6

14

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.

Chessy answered 2/9, 2011 at 10:20 Comment(0)
N
24

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);
Nimitz answered 2/9, 2011 at 10:32 Comment(1)
Don't forget add android:textAllCaps="false" (it doesn't work without that thing)Result
I
33

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

Illyrian answered 2/9, 2011 at 10:36 Comment(2)
this will bold the whole text in the button :(Chessy
I ask my master ,he say that Html.fromHtml is better and more easlier control.Illyrian
N
24

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);
Nimitz answered 2/9, 2011 at 10:32 Comment(1)
Don't forget add android:textAllCaps="false" (it doesn't work without that thing)Result
C
4

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

Christoforo answered 2/9, 2011 at 10:24 Comment(0)
L
3

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);
Lamed answered 2/9, 2011 at 10:49 Comment(0)
C
2

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

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

See Styling with HTML markup

Chiccory answered 2/9, 2011 at 10:29 Comment(0)
L
1

If want to set text programmatically then use this method

Button

First you have to set button's property in XML

android:textAllCaps="false" // very important without this property might be it won't show effect

public SpannableString setSpanableString(String textString, int start, int end){

    SpannableString spanString = new SpannableString(textString);
    spanString.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);

    return spanString;
}


Button btn; // get your button reference here

String text = "Hi, Dharmbir";
btn.setText(setSpanableString(text, 4, text.length));// set here your index

TextView

TextView tv; // get your TextView reference here

String text = "Hi, Dharmbir";
tv.setText(setSpanableString(text, 4, text.length));

Output

Hi, Dharmbir

Louielouis answered 24/12, 2016 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.