cannot SetBackground Resource image for android buttons <p>I want button with two states:</p> <ol> <li>OFF </li> <li>ON(or click).</li> </ol> <p>I have set normal image in the button background and I am trying to change image(pressed) from <code>onClick</code> method, but it doesn't change.</p> <pre><code>final Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { private int flag; @Override public void onClick(View v) { if(flag==1) { button1.setBackgroundResource(R.drawable.on); flag=0; } if(flag==0) { button1.setBackgroundResource(R.drawable.off); flag=1; } } }); </code></pre>