21

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);}
    }
   });
Zain
  • 37,492
  • 7
  • 60
  • 84
stealthcopter
  • 13,964
  • 13
  • 65
  • 83
  • Are you sure it is not getting called? perhaps add a logcat call into the onfocuschange method. I am guessing that it may be called many times (cancelling out the bold) – DEzra May 09 '10 at 16:31

3 Answers3

36

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);
             }
         });
DEzra
  • 2,978
  • 5
  • 31
  • 50
4

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!

Henry
  • 816
  • 5
  • 17
  • 3
    Are there any tricks to getting a selector to work with style? I have not been able to make this work in my code. – Carl Manaster Jun 07 '10 at 23:14
  • The theory in this sounds good, but the reality is aapt chokes when trying to use something like this (at least based on my limited tests). I'd like to be able to accomplish something similar to this, and it just doesn't seem to be that straight-forward. – Joe Jun 03 '11 at 21:26
  • And how do you reference the selector from your layout? Where do you put your selector file in? – ejel Nov 10 '12 at 01:57
  • 18
    This does not actually appear possible, as the statelist stuff has to be in color or drawable, and the top-level tag 'selector' throws an error when the xml file is in values/. This answer should not have been accepted, because it doesn't work, or doesn't give enough information to make it work. – Andrew Toulouse Dec 14 '12 at 00:06
  • 5
    It seems this has helped the OP and a lot of other people. The person who flagged this as not an answer should explain the flag. I hope this hasn't been upvoted by 13 people just because it sounded nice. – markus Jan 09 '13 at 15:35
  • 1
    doest work with style in selector. why this been a accepted answer – Krishna Shrestha Mar 12 '13 at 04:51
  • @markus it sure appears this way. This answer doesn't work, it's not a supported Android construct as the other comments imply. – botteaap Mar 14 '13 at 22:08
  • 1
    My above answer is more than 3 years old. It doesn´t work anymore :( – Henry Jul 12 '13 at 12:23
  • 3
    This accepted solution does not work! "Selector works only for drawables, not text appearances. There is currently not plan to make this happen." https://code.google.com/p/android/issues/detail?id=8941 – pandre Mar 06 '15 at 10:45
  • I am flagging this as not an answer – Kishan Solanki Aug 26 '20 at 10:33
3

Styles are not allowed in selectors. Reference


And to make the text bold, use this code:

btn_reset.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            // When the user clicks the Button
            case MotionEvent.ACTION_DOWN:
                btn_reset.setTypeface(Typeface.DEFAULT_BOLD);
                break;

            // When the user releases the Button
            case MotionEvent.ACTION_UP:
                btn_reset.setTypeface(Typeface.DEFAULT);
                break;
        }
        return false;
    }
});
Faraz
  • 2,144
  • 1
  • 18
  • 28