Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to make button text bold when pressed or focussed

Tags:

I want to change the text inside a button to be bold when the button is highlighted or pressed. I currently use a xml file to define the button and use the XML to change how it looks when pressed but I would like to do this without using an image.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
          android:state_pressed="false" 
          android:drawable="@drawable/reset_hover" />
    <item android:state_focused="true" 
          android:state_pressed="true"
          android:drawable="@drawable/reset_hover" />
    <item android:state_focused="false" 
          android:state_pressed="true"
      android:drawable="@drawable/reset_hover" />
    <item android:drawable="@drawable/reset" />
</selector>

I tried using something like the following, but it doesn't seem to ever get called.

    final Button btn_reset = (Button) findViewById(R.id.btn_reset);
    btn_reset.setOnClickListener(this); 
    btn_reset.setOn(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){btn_reset.setTypeface(null, Typeface.BOLD);}
        else{btn_reset.setTypeface(null, Typeface.NORMAL);}
    }
   });
like image 897
stealthcopter Avatar asked Apr 21 '10 10:04

stealthcopter


People also ask

How do I make parts of text bold on Android?

android:textStyle attribute is the first and one of the best way to make the text in TextView bold. just use “bold”. If you want to use bold and italic. Use pipeline symbol “|” .

How do I set bold span in Android?

For example, the StyleSpan can be used like this: SpannableString string = new SpannableString("Bold and italic text"); string. setSpan(new StyleSpan(Typeface. BOLD), 0, 4, Spannable.

What is bold text in Android?

Assuming you are a new starter on Android Studio, Simply you can get it done in design view XML by using android:textStyle="bold" //to make text bold android:textStyle="italic" //to make text italic android:textStyle="bold|italic" //to make text bold & italic. Follow this answer to receive notifications.


2 Answers

You could try putting the bold code inside the click event for the button:

final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Set bold on click
                 button.setTypeface(null, Typeface.BOLD);
             }
         });
like image 140
DEzra Avatar answered Sep 20 '22 15:09

DEzra


Attention! See update below


You could create your own style.xml in which you define the text style. In your selector you can reference to the style. style.xml

<style name="myStyle">  
    <item name="android:textSize">9px</item>
    <item name="android:gravity">center_horizontal</item>
    <item name="android:textColor">#fff</item>
    <item name="android:textStyle">bold</item>
</style>

And in your selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
      android:state_pressed="false" 
      style="@style/myStyle" /> </selector>

Update 2020: My answer is more than 10 years old. It doesn´t work anymore!

like image 29
Henry Avatar answered Sep 18 '22 15:09

Henry