Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Checking Background Resource Drawable

Please help how can we do a Background Resource Check?

Example:

  Button button1;

  protected void onCreate(Bundle savedInstanceState) {
  ..........................................
  button1 = (Button) findViewById(R.id.example1);
  }

  public void onclick1 (View v){
         button1.setBackgroundResource(R.drawable.example2);
  }

  public void onclick2 (View v){

My Question Here, checking if button1 button drawable = example2

        if (..........................){
               //Action
        }

If not, when clicked will done another action

        else {
               //Another Action
        }
   }
like image 393
user2341387 Avatar asked Jun 17 '26 06:06

user2341387


1 Answers

You can use the getBackground() method of the View class for both buttons and compare it like this:

if (button1.getBackground().getConstantState().equals(button2.getBackground().getConstantState())) {
    }
else {
    }
like image 60
Ahmad Avatar answered Jun 20 '26 11:06

Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!