267

I made a button that changes the background drawable on different states, this way:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true" android:drawable="@drawable/btn_location_pressed" /> <!-- pressed -->
     <item android:state_focused="true" android:drawable="@drawable/btn_location_pressed"/> <!-- focused -->
     <item android:drawable="@drawable/btn_location"/> <!-- default -->
</selector>

The problem here is that I'm also trying to change the textColor as I do with the drawable but I'm not being able to. I already tried android:textColor and android:color but the first doesn't work whilst the seconds changes my background.

The next code is part of my layout. Regarding to the text color it only works for the normal state text color, thus not changing it to the white one while pressed

<Button android:id="@+id/location_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:background="@drawable/location"          
        android:textSize="15sp"
        android:textColor="@color/location_color"
        android:textColorHighlight="#FFFFFF"
   />

Has anybody got a clue?

Zain
  • 37,492
  • 7
  • 60
  • 84
dwbrito
  • 5,194
  • 5
  • 32
  • 48

5 Answers5

599

Create a stateful color for your button, just like you did for background, for example:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Focused and not pressed -->
    <item android:state_focused="true" 
          android:state_pressed="false" 
          android:color="#ffffff" />

    <!-- Focused and pressed -->
    <item android:state_focused="true" 
          android:state_pressed="true" 
          android:color="#000000" />

    <!-- Unfocused and pressed -->
    <item android:state_focused="false" 
          android:state_pressed="true" 
          android:color="#000000" />

    <!-- Default color -->
    <item android:color="#ffffff" />

</selector>

Place the xml in a file at res/drawable folder i.e. res/drawable/button_text_color.xml. Then just set the drawable as text color:

android:textColor="@drawable/button_text_color"
ZooMagic
  • 616
  • 7
  • 15
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • 15
    Note that (for me, at least) there is a bug where the "normal" state ( in your answer) must be placed at the end of the file as in your answer. Placing the normal state at the top of the file (above the other states) stops the selector from working. – Chris Blunt Jul 11 '11 at 08:25
  • 62
    it's not a bug. It's the way state selection is supposed to work. It's not a _best match_, instead the first that fits will make it through. – superjos Dec 05 '11 at 14:25
  • Do you how to do this with an integer value? I am trying to do something similar with the text padding. – elimirks Mar 28 '14 at 17:35
  • spent some time trying this to no avail, then discovered that I had still been setting it to the background property instead of the textcolor property. Not used to seeing textcolor take a drawable! – Odaym Aug 27 '14 at 18:27
  • 22
    It beter if the color selector is located in `res/color` folder. And when call, use: `android:textColor="@color/button_text_color"` – Justin Oct 23 '14 at 04:55
  • I noticed that there are a lot of different properties to make use of in the selector item. In my case, I made use of ` ` to create two states that represent whenever a switch is on or off. Thanks for the help! – Saulo Aguiar Mar 21 '16 at 08:27
  • 1
    it is works but its shows warning that tab required a 'drawable' attribute or child tag defining drawable. – vishal patel May 11 '17 at 11:46
  • Using this solution when I click on any Button for just a fraction of a second the color change happens and after that, it turns to its original color. Does anyone face a similar issue? – iamdhavalparmar Apr 06 '22 at 09:32
18

Another way to do it is in your class:

import android.graphics.Color; // add to top of class  

Button btn = (Button)findViewById(R.id.btn);

// set button text colour to be blue
btn.setTextColor(Color.parseColor("blue"));

// set button text colour to be red
btn.setTextColor(Color.parseColor("#FF0000"));

// set button text color to be a color from your resources (could be strings.xml)
btn.setTextColor(getResources().getColor(R.color.yourColor));

// set button background colour to be green
btn.setBackgroundColor(Color.GREEN);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • 1
    This will change the text color on the button, but will stay that color during the different states of the button (e.g. pressed). In most scenarios, when a button's background color changes during a state, it is also desired to change the button's text color as well, this is where @Konstantin Burov's answer comes handy. – Dzhuneyt Sep 19 '14 at 20:07
  • 1
    This does not answer the original question. The question is about how to define state based colors to a text view just like you can set state based drawables. – alchemist Jun 20 '17 at 07:24
5

ok very simple first go to 1. res-valuse and open colors.xml 2.copy 1 of the defined text their for example #FF4081 and change name for instance i changed to white and change its value for instance i changed to #FFFFFF for white value like this

<color name="White">#FFFFFF</color>

then inside your button add this line

 b3.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.White));

ok b3 is the name of my button so changed of the name of ur button all others will be same if u use white color if you change different color then change white to the name of your color but first you have define that color in colors.xml like i explained in pont 2

SeekingKnowleges
  • 123
  • 2
  • 10
1

Changing text color of button

Because this method is now deprecated

button.setTextColor(getResources().getColor(R.color.your_color));

I use the following:

button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));
Gjoko Bozhinov
  • 1,137
  • 11
  • 15
0

Use getColorStateList like this

setTextColor(resources.getColorStateList(R.color.button_states_color))

instead of getColor

setTextColor(resources.getColor(R.color.button_states_color))
Li Jin
  • 1,879
  • 2
  • 16
  • 23