53

I read a couple of posts but none of them had the working solution.

Once you do

button.setBackgroundColor(0x00000000);

How do you revert the button's background color back to default color?

coolcool1994
  • 3,704
  • 4
  • 39
  • 43
  • 1
    Possible duplicate of [How to get a Button's background back to default (programmatically)?](http://stackoverflow.com/questions/6471932/how-to-get-a-buttons-background-back-to-default-programmatically) – GSerg Mar 28 '16 at 17:14

4 Answers4

76

use:

btn.setBackgroundResource(android.R.drawable.btn_default);
Sean
  • 5,176
  • 2
  • 34
  • 50
  • 3
    button.setBackgroundColor(0x00000000); button.setBackgroundColor(android.R.drawable.btn_default); This does not change the color back to the normal color – coolcool1994 Feb 10 '13 at 20:49
  • 1
    Oh Thank you so much!! I read android.R.drawable.btn_default before but I didn't know it was supposed to be setBackgroundResource. Thank you so much +1 to you everything! – coolcool1994 Feb 10 '13 at 21:00
  • 4
    this resets my button to a Android 2.3 version, how do I make it reset to android version default? – Sartheris Stormhammer Sep 09 '14 at 06:58
  • @Sartheris - Make sure in your manifest that the "target" is the new API you are aiming for (no reason it shouldn't be 19) – Sean Sep 10 '14 at 07:18
  • @Sean Setting the target in the manifest does not affect it. I can set both the target and the minimum to 4.0.3 (15), and it still resets the button to something like 2.3 (10). – GSerg Mar 28 '16 at 17:22
  • doesn't work for Marshmallow material buttons either – msrd0 Oct 12 '17 at 21:33
  • This will set a yellow color on the pressed state for the button in API 27 – Paixols Aug 31 '18 at 00:55
21

If the background color was set using

btn.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);

it can be reset using:

btn.getBackground().clearColorFilter();

In contrast to button.setBackgroundColor() setting the color this way preserves the button's shape.

Ivo
  • 1,768
  • 20
  • 19
  • this answer worked better for me with appcompat api16+ – sivi Feb 20 '18 at 09:58
  • for some reason this does not work for me when trying to change the background of an EditText. I wanted a way to change background of the input box, then reset it back to whatever it had originally. Setting the background color directly overwrites the original background drawable and there is no way to reset it without keeping a copy of the original. – Johnny Nov 04 '18 at 16:28
  • clearColorFilter does not reset the color to the default grey, but to white. – user1785730 Dec 22 '18 at 16:44
10

Nobody mentioned TRANSPARENT use it like this

findViewById(R.id.button_id).setBackgroundColor(Color.TRANSPARENT);

Thank me later

Sahil Paudel
  • 301
  • 7
  • 13
5

this worked better for me :

Button defbtn=new Button(this);
btn.setBackground(defbtn.getBackground());
Omid Fast
  • 183
  • 4
  • 11