Is there a way to change the color of the button in android studio?

-1

I was testing the following:

if(x==1){
  button.setColor(#D32F2F);
  // o de algo a si 
  button.setBackground(#D32F2F);
}

I do not know if they understand me since I am something new in this.
The buttons I want them to change color depending on the state if 0 : red, if it is 1 : green and if it is 3 : gray.

    
asked by Omar Campos 05.06.2018 в 17:18
source

1 answer

1

If you can change it, this should work:

Button btn = (Button) findViewById(R.id.push_button);
//un archivo drawable
btn.setBackground(ContextCompat.getDrawable(context, R.drawable.my_bg));
//o un color
btn.setBackground(ContextCompat.getColor(context, R.color.my_color));
    
answered by 05.06.2018 в 17:41