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.
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.
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
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>
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.