Home » Android » android – Programmatically change button background drawable onClick

android – Programmatically change button background drawable onClick

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to toggle my button’s background drawables, so that when the user clicks the button its background is changed and when the user clicks the button again its background returns to defaul. Here is my code:

public void Favorites(View V) {
  Button star = (Button) findViewById(R.id.buttonStar);
  if(star.getBackground().equals(R.drawable.btn_star_off)) {
    star.setBackgroundResource(R.drawable.btn_star_on);
  } else {               
    star.setBackgroundResource(R.drawable.btn_star_off);
  }
}

I am pretty sure this is not how you use drawables with if statements. Can someone suggest a way to do it?

How to&Answers:
private boolean isButtonClicked = false; // You should add a boolean flag to record the button on/off state

protected void onCreate(Bundle savedInstanceState) {
    ......
    Button star = (Button) findViewById(R.id.buttonStar);
    star.setOnClickListener(new OnClickListener() { // Then you should add add click listener for your button.
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.buttonStar) {
                isButtonClicked = !isButtonClicked; // toggle the boolean flag
                v.setBackgroundResource(isButtonClicked ? R.drawable.btn_star_on : R.drawable.btn_star_off);
            }
        }
    });
}

Answer:

You can create an xml in the drawable folder.
This xml (you choose the name…let’s call it “bg_button_star.xml”) could look just like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/btn_star_on" />
<item android:drawable="@drawable/btn_star_off" />

Then you have to assign this drawable file to the Button background property in the layout file.

android:background="@drawable/bg_button_star"

If you want to do this programmatically then you just have to do:

button.setBackgroundResource(R.drawable.bg_button_star);

When the user click the first time on the button, you set the Selected state to ‘true’. The background changes accordingly. (viceversa for the ‘false’ Selected state).

Answer:

You can do in your onClick() something like:

if(star.getTag()==R.drawable.btn_star_on){
    star.setTag(R.drawable.btn_star_off);
    star.setBackgroundResource(R.drawable.btn_star_off);
} else {
    star.setTag(R.drawable.btn_star_on);
    star.setBackgroundResource(R.drawable.btn_star_on);
}

Obviously it’s better to the the tag before the if and else statement based on your informations. I don’t know the rest of your code and how you check if this button has to be iniziatilized with the drawable resource btn_star_off or btn_star_on

Answer:

You can try this.

public void Favorites(View V) {
Button star = (Button) findViewById(R.id.buttonStar);

if(star.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.btn_star_off).getConstantState())) 
    {
            star.setBackground(R.drawable.btn_star_on);
    } else {               
            star.setBackground(R.drawable.btn_star_off);
  }
}

But make sure you are calling this method onClick() of the start button.

Other wise you have to do something like this.

Button star = (Button) findViewById(R.id.buttonStar);
star.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

      if(v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.btn_star_off).getConstantState())) 
       {
            v.setBackground(R.drawable.btn_star_on);
       } else {               
            v.setBackground(R.drawable.btn_star_off);
       }
        }
    });

Answer:

Dont do it like that. Use a selector resource instead http://www.compiletimeerror.com/2014/03/android-button-selector-tutorial-with.html

Answer:

In this case, instead of using Button you should use ToggleButton.

There is a API Guide for it:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html