androidbuttonboldtypeface

Android: Bold Button Text


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.


Solution

  • 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);