title: Android Java Multiple ImageButton array OnClick and toggle background colour
body: <p>I have tried some different solutions to this question, but still not found a suitable smart solution. On the activity there will be up to 20 ImageButton's and when the user presses the button the background changes colour, all very simple. But it seems like a lot of code to do something very simple.</p>
  <p>With the other responses it seems you can have a common OnClick from the ImageButton XML, but there is still a massive switch statement checking for each ImageButton. I have tried an array of OnClickListners(), but cannot work out which Image Button has been pressed.</p>
  <p>If the only way is a very long &quot;if&quot; statement or &quot;switch&quot; case then just copy &amp; paste the code, but just wondering if there is an easier and more smarter way to manage these multiple ImageButtons</p>
  <pre><code>    ElementButton0 = (ImageButton) findViewById(R.id.elementButton0);
      ElementButton1 = (ImageButton) findViewById(R.id.elementButton1);
      ElementButton2 = (ImageButton) findViewById(R.id.elementButton2);
  
      ElementButton0.setOnClickListener(new View.OnClickListener()
      {
          @Override
          public void onClick(View view) {
              if (ElementButton0.isPressed() == true) {
                  if (elementbuttonstate0 == true)
                  {
                      ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                      elementbuttonstate0 = false;
                  } else {
                      ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
                      elementbuttonstate0 = true;
                  }
              }
  
          }
      });
  
      ElementButton1.setOnClickListener(new View.OnClickListener()
      {
          @Override
          public void onClick(View view) {
              if (ElementButton1.isPressed() == true) {
                  if (elementbuttonstate1 == true)
                  {
                      ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                      elementbuttonstate1 = false;
                  } else {
                      ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
                      elementbuttonstate1 = true;
                  }
              }
  
          }
      });
      ElementButton2.setOnClickListener(new View.OnClickListener()
      {
          @Override
          public void onClick(View view) {
              if (ElementButton2.isPressed() == true) {
                  if (elementbuttonstate2 == true)
                  {
                      ElementButton2.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                      elementbuttonstate2 = false;
                  } else {
                      ElementButton2.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
                      elementbuttonstate2 = true;
                  }
              }
  
          }
      });
  </code></pre>