How to change the color of the boot via code on android?

Asked

Viewed 12,679 times

1

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

3 answers

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 has to be in the res->values->color_list.xml us Resources:

<color name="Red">#FF0000</color>

2


Use:

seuButton.setBackgroundResource(R.drawable.my_button_style);

With this method, you will assign to Background from 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 in: Porterduffcolorfilter - Documentation

-1

Use this within the code XML of Button to switch to the desired color:

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

To change color without code (even if you haven’t asked this, I’ll write to maybe help those who need) go to Desing>Button>Background>Resources(ellipsis next to Background), then just click on the chosen color.

  • The question is actually how to change the color of the button via java code. It wants to change the color on the mouseover.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.