how to change the color of the button via code in android?

1

How to change the color of the button via code, because when touching the button I want it to change the color, the color is inside the drawable.

    
asked by anonymous 18.02.2015 / 18:49

3 answers

2

Use:

seuButton.setBackgroundResource(R.drawable.my_button_style);

With this method, you will assign to the Background of the button, a shape coming XML with your desired color / style.

But with this solution, you will apply a color filter without needing an XML-Drawable.

PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
seuButton.getBackground().setColorFilter(colorFilter);

More on : PorterDuffColorFilter - Documentation

    
18.02.2015 / 18:55
3

You can change the color of the button like this:

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

botao.setBackgroundResource(R.color.Red);

The color must be in res->values->color_list.xml in resources :

<color name="Red">#FF0000</color>
    
18.02.2015 / 18:59
-1

Use this within the XML tag of the Button to change to the desired color:

android:background="@android:color/(coloque sua cor aqui, exemplo: holo_blue_dark)"

To change color without being by code (even if you did not ask this, I'll write to maybe help those who need it) go to Desing> Button> Background & ), then just click on the chosen color.

    
13.12.2017 / 01:19