title: How to change background of an ImageButton with out clicking?
body: <p>I have four image buttons and I want to change their backgrounds programmatically without having to use use an onClickListener.</p>
  
  <pre><code>public class playactivity extends Activity {
  
  private ImageButton first;
  private ImageButton second;
  private ImageButton third;
  private ImageButton fourth;
  private int stage1;
  private database db1;
  static boolean sw=false;
  static boolean sw1=false;
  static boolean sw2=false;
  static int c1,c2,c3,c4=0;
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.playactivity);
      first=(ImageButton)findViewById(R.id.firstbtn);
      second=(ImageButton)findViewById(R.id.secbtn);
      third=(ImageButton)findViewById(R.id.thirdbtn);
      fourth=(ImageButton)findViewById(R.id.fourthbtn);
  
       db1=new database(this);
  
        db1.useable();
        db1.open();
        stage1=db1.newstage(1);
        //stages of game
  
      if(stage1 == 1)
          second.setImageResource(R.drawable.unlock);
      else if(stage1 == 2)
      {
          second.setImageResource(R.drawable.unlock);
          third.setImageResource(R.drawable.unlock);
      }
  
      else if(stage1 == 3)
      {
          second.setImageResource(R.drawable.unlock);
          third.setImageResource(R.drawable.unlock);
           fourth.setImageResource(R.drawable.unlock);
      }
      first.setOnClickListener(new OnClickListener(){
          public void onClick(View arg0) {
  
              Intent first1=new Intent(playactivity.this,Main.class);
              startActivity(first1);
              c1++;
  
  }
       });
  
      second.setOnClickListener(new OnClickListener(){
          public void onClick(View arg0) {
              if(sw){
  
              Intent first1=new Intent(playactivity.this,Main.class);
              startActivity(first1);
              c2++;
          //  second.setImageResource(R.drawable.unlock); &lt;--
  
              }
  }
       });
  
  
      third.setOnClickListener(new OnClickListener(){
          public void onClick(View arg0) {
              if(sw1){
  
              Intent first1=new Intent(playactivity.this,Main.class);
              startActivity(first1);  
              c3++;
         //third.setImageResource(R.drawable.unlock); &lt;--
              }
  
  }
       });
      fourth.setOnClickListener(new OnClickListener(){
          public void onClick(View arg0) {
              if(sw2){
  
              Intent first1=new Intent(playactivity.this,Main.class);
              startActivity(first1);
              c4++;
           // fourth.setImageResource(R.drawable.unlock); &lt;--
              }
  
  }
       });
  </code></pre>
  
  <p>}}</p>
  
  <p>This code doesn't work, strangely enough though, I'm able to change the buttons' backgrounds using an onClickListener like this :</p>
  
  <pre><code>second.setOnClickListener(new OnClickListener() {
  
      public void onClick(View view) {
          if (sw) {
              Intent intent = new Intent(PlayActivity.this, Main.class);
              startActivity(intent);
              c2++;
              second.setImageResource(R.drawable.unlock);
          }
      }
  });
  
  
  third.setOnClickListener(new OnClickListener(){
  
      public void onClick(View view) {
          if (sw1) {
              Intent intent = new Intent(PlayActivity.this, Main.class);
              startActivity(intent);  
              c3++;
              third.setImageResource(R.drawable.unlock);
          }
      }
  });
  </code></pre>