1

I currently have 8 imageButtons set up with id's imageButton1 to imageButton8.

When i click on a particular button (imageButton2 for example) I want to loop through my images in the drawable folder named image1....image10, and set the image to the imageButton for each click.

I have looked at various for loops to solve this but there they have led nowhere. Here is a snippet of a for loop attempt:
UPDATED 26/4/16
SOLVED

//UPDATED 25/4/16
ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
ImageButton[] btns;
int[] drawables = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8};


@Override
protected void onCreate(Bundle savedInstanceState) {  

    btn1 = (ImageButton) findViewById(R.id.imageButton1);
    btn2 = (ImageButton) findViewById(R.id.imageButton2);
    btn3 = (ImageButton) findViewById(R.id.imageButton3);
    btn4 = (ImageButton) findViewById(R.id.imageButton4);
    btn5 = (ImageButton) findViewById(R.id.imageButton5);
    btn6 = (ImageButton) findViewById(R.id.imageButton6);
    btn7 = (ImageButton) findViewById(R.id.imageButton7);
    btn8 = (ImageButton) findViewById(R.id.imageButton8);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
}


btns = new ImageButton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8};
}

int counter = 0;
        @Override
    public void onClick(View v)
        {

            counter ++;


            for (int i = 0; i < btns.length; i++)
            {


                if (v.getId() == btns[i].getId())
                {
                    if(counter ==8)
                    {
                        counter =0;
                    }

                        ((ImageButton)v).setImageResource(drawables[counter]);

                }

            }
}
}

UPDATED 26/4/16 SOLVED

Another attempt was to create a counter to store the number of clicks and set if statements to change the images according to each click.

The problem here was it creates a huge amount of code considering i have multiple buttons.

On top of that when i run this on two different buttons, once i tap on imageButton1 a few times, then imageButton2 a few times, imageButton1 doesn't respond anymore.

int counter =0;

        @Override
    public void onClick(View v)
        {
            counter++;


            switch(v.getId())
            {
                case R.id.imageButton1:
                    if(counter == 1){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image1);
                        break;

                    }else if (counter == 2){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image2);
                        break;

                    }else if (counter == 3){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image3);

                        break;

                    }else if (counter == 4){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image4);

                        break;
                    }else if (counter == 5){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image5);
                        break;

                    }else if (counter == 6){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image6);

                        break;

                    }else if (counter == 7){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image7);

                        break;
                    }else if (counter == 8){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image8);
                        break;

                    }else if (counter == 9){

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image9);

                        break;

                    }else if (counter == 10) {

                        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
                        btn1.setImageResource(R.drawable.image10);
                        counter = 0;
                        break;
                    }
                    break;

I would appreciate any guidance, meanwhile if i solve this issue i will repost an Edit. Cheers

Steven
  • 13
  • 6

2 Answers2

0

Just a suggestion.

Maybe you could declare the buttons and image resources as arrays then loop through them.

For example:

public class Sample extends Activity {

  ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
  ImageButton[] btns;
  int[] drawables = new int[]{R.drawable.imageButton1,R.drawable.imageButton2,R.drawable.imageButton3,R.drawable.imageButton4,R.drawable.imageButton5,R.drawable.imageButton6,R.drawable.imageButton7,R.drawable.imageButton8};
  int[] origDrawables = new int[]{R.drawable.imageOrigButton1,R.drawable.imageOrigButton2,R.drawable.imageOrigButton3,R.drawable.imageOrigButton4,R.drawable.imageOrigButton5,R.drawable.imageOrigButton6,R.drawable.imageOrigButton7,R.drawable.imageOrigButton8};

  @Override
  public void onCreate(Bundle saveInstanceState) {
    btn1 = (ImageButton) findViewById(R.id.imageButton1);
    btn2 = (ImageButton) findViewById(R.id.imageButton2);
    btn3 = (ImageButton) findViewById(R.id.imageButton3);
    btn4 = (ImageButton) findViewById(R.id.imageButton4);
    btn5 = (ImageButton) findViewById(R.id.imageButton5);
    btn6 = (ImageButton) findViewById(R.id.imageButton6);
    btn7 = (ImageButton) findViewById(R.id.imageButton7);
    btn8 = (ImageButton) findViewById(R.id.imageButton8);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
    btn6.setOnClickListener(this);
    btn7.setOnClickListener(this);
    btn8.setOnClickListener(this);

    btns = new ImageButton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8};
  }

  @Override
  public void onClick(View v) {
    for (int i = 0; i < btns.length; i++) {
      if (v.getId == btns[i].getId) {
        ((ImageButton)v).setImageResource(drawables[i]);
      } else {
        ((ImageButton)v).setImageResource(origDrawables[i]);
      }
    }
  }

}

NOTE: Pardon any code/syntax errors as I did this using notepad.

ads
  • 1,703
  • 2
  • 18
  • 35
  • I have tried something like this before but an error appears displaying Cannot resolve "setImageResource(int)" – Steven Apr 21 '16 at 00:35
  • Updated my answer with this "((ImageButton)v).setImageResource(drawables[i]);". I think the reason for the error "Cannot resolve" is because View object does not have it. So we might need to cast it to ImageButton to get the method. – ads Apr 21 '16 at 08:51
  • public void onClick(View v) { for (int i = 0; i < btns.length; i++) { if (v.getId() == btns[i].getId()) { ((ImageButton)v).setImageResource(drawables[i]); } } Here is my code, now it shows no errors but when it runs it crashes. Note i have replaced your getItemId with getId() – Steven Apr 21 '16 at 08:52
  • at android.view.View.performClick(View.java:4760) at android.view.View$PerformClick.run(View.java:19774) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5218) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) – Steven Apr 21 '16 at 09:30
  • made some corrections in the code. can you try again. – ads Apr 22 '16 at 01:15
  • When i run the app i can only click each button once and btn1 changes to image1, btn2 image2 ....etc. So i need each button to go through an infinite loop from image1 -8 and back. – Steven Apr 23 '16 at 14:02
  • maybe you can also define an array variable for your original image resource. then on the loop, if button clicked is not equal to the iterated button, set it to the original image resource. see my updated answer above. – ads Apr 25 '16 at 00:55
  • I take it R.drawable.imageButton1 should be R.drawable.image1? My drawable folder contains 8 images from image1 to image8. I don't understand what values imageOrigButton1 etc. have? I changed my original code slightly to this – Steven Apr 25 '16 at 08:01
  • btns = new ImageButton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8}; } int counter = 0; @Override public void onClick(View v) { counter ++; for (int i = 0; i < btns.length; i++) { if (v.getId() == btns[i].getId()) { if(counter ==8) { counter =0; } ((ImageButton)v).setImageResource(drawables[i+counter]); } } – Steven Apr 25 '16 at 08:06
  • It uses a counter. It loops through the images infinitely for imageButton1 no problem but any other button it will crash after it reaches the 8th image, i think it has something to do with resetting the counter. But im one step away from getting it to work. Thank you for you help so far. – Steven Apr 25 '16 at 08:06
  • Sorry i couldn't get the code lined up i will update my original – Steven Apr 25 '16 at 08:07
  • Problem Solved. I removed the i variable from the setImageResources(). See edit. Thanks again for the help. – Steven Apr 26 '16 at 19:39
-1

Try this

    int counter = 1;
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.imageButton1:
             ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
               if(counter == 1){
                    btn1.setImageResource(R.drawable.image1);
                }else if (counter == 2){
                    btn1.setImageResource(R.drawable.image2);
                }else if (counter == 3){
                    btn1.setImageResource(R.drawable.image3);
                }else if (counter == 4){
                    btn1.setImageResource(R.drawable.image4);
                }else if (counter == 5){
                    btn1.setImageResource(R.drawable.image5);
                }else if (counter == 6){
                    btn1.setImageResource(R.drawable.image6);
                }else if (counter == 7){
                    btn1.setImageResource(R.drawable.image7);
                }else if (counter == 8){
                    btn1.setImageResource(R.drawable.image8);
                }else if (counter == 9){
                    btn1.setImageResource(R.drawable.image9);
                }else if (counter == 10) {
                    btn1.setImageResource(R.drawable.image10);
                }

            counter = (counter % 10) + 1 ;
                break;
    }
jgm
  • 1,230
  • 1
  • 19
  • 39
  • ?? You're doing it wrong... Having all those `if...else` inside the switch is wrong. Check the button with an `if` first, then put the switch inside. – CaptJak Apr 19 '16 at 23:48
  • @CaptJak Is that an issue ? Why cant I put `if..else` inside `switch` ? – jgm Apr 19 '16 at 23:52
  • It is possible to do that, it will work, but if you are doing it this way, why are you using the switch? Why are you using `if...else`? Why don't you just use one or the other? For performance reasons, you should use a switch. See: https://stackoverflow.com/questions/6705955/why-switch-is-faster-than-if – CaptJak Apr 20 '16 at 00:29