title: How to connect multiple of buttons in the same onclicklistener?
body: <p>What I mean is not each of the buttons do each individual work. What I mean is that like there are many buttons. Then 4 buttons is choose randomly in an order from the bunch of buttons. Then the user had to click the chosen 4 buttons in the same order and then something will happen. This is the code I had done.</p>
  
  <pre><code>OnClickListener click2 = new OnClickListener() {            
      @Override
      public void onClick(View v) {
          if (v==ans1)
          {
              if (v==ans2)
              {
                   if (v==ans3)
                   {
                      if (v==ans4)
                      {
                          tv1.setText("You Win");
                      }
                   }
               }               
          }           
      }
  }; 
  </code></pre>
  
  <p>My code does not work at all, it will only work if all the 4 buttons are the same button. Is there any method to solve this by using the same <code>OnClickListener</code> for all the 4 buttons. Or is there some other method to get many <code>View</code> on 1 <code>OnClickListener</code>?</p>
  
  <p>This is the whole code</p>
  
  <pre><code>public class MemoryActivity extends Activity  {
      ImageButton btn1, btn2, btn3, btn4, btn5;
      ImageButton ans1, ans2, ans3, ans4, ans5;
      TextView tv1, tv2, tv3, tv4;
      Button start;
      Random random = new Random();
      Handler handler1 = new Handler();
      private List&lt;Integer&gt; mClickedButtonIds = new ArrayList&lt;Integer&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_memory);
  
          tv1 = (TextView)findViewById(R.id.textView1);
          tv2 = (TextView)findViewById(R.id.textView2);
          tv3 = (TextView)findViewById(R.id.textView3);
          start = (Button)findViewById(R.id.button1);
          start.setOnClickListener(click1);
  
          btn1 = (ImageButton)findViewById(R.id.imageButton1);
          btn1.setOnClickListener(click2);
          btn2 = (ImageButton)findViewById(R.id.imageButton2);
          btn2.setOnClickListener(click2);
          btn3 = (ImageButton)findViewById(R.id.imageButton3);
          btn3.setOnClickListener(click2);
          btn4 = (ImageButton)findViewById(R.id.imageButton4);
          btn4.setOnClickListener(click2);        
      }       
  
      private OnClickListener click1 = new OnClickListener() {
  
          @Override
          public void onClick(View v) { 
              final ImageButton[] all= {btn1, btn2, btn3, btn4};
  
              for (int a=0; a&lt;4; a++) {                   
                  final int b = a;
  
                  handler1.postDelayed(new Runnable() { //start delay a           
                      @Override
                      public void run() { // actions start                                                    
                          btn5 = all[random.nextInt(all.length)];
  
                          if (b==0) {ans1 = btn5;}
                          else if (b==1) {ans2 = btn5;}
                          else if (b==2) {ans3 = btn5;} 
                          else if (b==3) {ans4 = btn5;}
  
                          btn5.setBackgroundColor(Color.RED);
  
                          handler1.postDelayed(new Runnable() { //start delay b
                              @Override
                              public void run() {                 
                                  btn5.setBackgroundResource(R.drawable.btn_default_holo_dark); 
                              }
                          }, 500); //end delay b  
                  //action end             
  
                  }}, 1000*a ); //end delay a         
  
  
                  } 
              } 
          };
  
    OnClickListene click2 = new OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
  
  
          int[] mDesiredOrder = new int[] { ans1,ans2,ans3,ans4 }; //error here
  
          mClickedButtonIds.add(v.getId());       
          if (mClickedButtonIds.size() &gt; 4) 
          { 
              mClickedButtonIds.remove(0);            
  
              if (mClickedButtonIds.get(0) == mDesiredOrder[0] 
                  &amp;&amp; mClickedButtonIds.get(1) == mDesiredOrder[1]
              &amp;&amp; mClickedButtonIds.get(2) == mDesiredOrder[2]
              &amp;&amp; mClickedButtonIds.get(3) == mDesiredOrder[3] 
                  )
              {
                  tv1.setText("You Win");
              }
          }       
      } 
  
  
  
  
      }; 
  </code></pre>