user2341387
user2341387

Reputation: 303

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
        }
   }

Upvotes: 3

Views: 5695

Answers (1)

Ahmad
Ahmad

Reputation: 72563

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 {
    }

Upvotes: 5

Related Questions