0

I have a button and typically when I click it this light blue color comes up around the button to show that you are touching down on it. How can I change this color?

I also want to change the default background color of this button which I do programmatically like this:

button.setBackgroundColor(Color.RED);

And when I do this I know it won't change the touchdown color which is what I am trying to do, but it makes the light blue color on touchdown go away which confuses me?

But my question is how to I change the touch down color?

I talking about this

This is not being pressed down just normal:

enter image description here

This is as I hold the button down

enter image description here

Thanks for the help!! :)

iqueqiorio
  • 1,149
  • 2
  • 35
  • 78

1 Answers1

0

Button backgrounds have states. To use a custom background, you have to define a selector xml file.

See Buttons#CustomBackground

tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • You would still have define a selector if you want your button to have different states. You can swap the selector at runtime by calling `button.setBackgroundResource(R.drawable.selector);` – tachyonflux Dec 07 '14 at 04:39
  • okay when I add the xml file in the example you posted I get cannot resolve `@drawable/button_pressed` – iqueqiorio Dec 07 '14 at 04:44
  • You have to create it, that's what makes it custom. Here is a very similar question: http://stackoverflow.com/questions/11085308/changing-the-background-drawable-of-the-searchview-widget – tachyonflux Dec 07 '14 at 04:50
  • You can take a look at the android sdk's button resources: `sdk\platforms\android-xx\data\res\drawable-mdpi` btn_default_disabled, btn_default_focused, btn_default_pressed – tachyonflux Dec 07 '14 at 05:08