Reputation: 75
I have a button with a drawable XML Background
android:background="@drawable/greenbut"
and i want to change the Background to my @drawable/yellowbut
by make an onclick on my button.
I tried it with this code, but it seems to be completely wrong. I cannot use the setBackground Function
. Can you please help..?
public void colorChanger(View v)
{
Button changeBut = (Button) findViewById(R.id.button2);
changeBut.setBackground(R.drawable.yellowbut);
changeBut.isClickable();
}
Upvotes: 0
Views: 506
Reputation: 801
Its as simple as that just follow the code hope it will help
setContentView(R.layout.activity_main);
final Button changeBut = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setBackgroundColor(Color.yellow);
}
});
Upvotes: 2
Reputation: 10697
Try this
public void colorChanger(View v)
{
Button changeBut = (Button) findViewById(R.id.button2);
changeBut.setBackgroundResource(R.drawable.yellowbut);
changeBut.isClickable();
}
Upvotes: 2