title: Programmatically change button background drawable onClick
body: <p>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:</p>
  
  <pre><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);
    }
  }
  </code></pre>
  
  <p>I am pretty sure this is not how you use drawables with <code>if</code> statements. Can someone suggest a way to do it?</p>