1

I want to put a timer on the imageSwitcher class. I was able to do that for 2 pictures using the following code, but I can't add more pictures. Can anyone tell me how can I add more pictures to the ImageSwitcher Timer?

imageSwitcher.postDelayed(new Runnable() {
            int i = 0;
            public void run() {
                imageSwitcher.setImageResource(
                    i++ % 2 == 0 ?
                        R.drawable.image1 :
                        R.drawable.mage2);
                imageSwitcher.postDelayed(this, 1000);
            }
        }, 1000);
Jarrod
  • 9,349
  • 5
  • 58
  • 73
  • are you looking for something like this ? http://codinglookseasy.blogspot.in/2012/08/image-slide-show.html – G_S Oct 01 '12 at 07:42
  • i just want to knw that how can i take more than 2 arguments in ImageSwitcher.setImageResource or how can i add more pics to it ? – Hassan Mujtaba Oct 01 '12 at 07:52
  • adding pics is discussed in setImagesToFlipper() of the link – G_S Oct 01 '12 at 07:55

1 Answers1

1

you can store in array

private static final int[] imgs = {R.drawable.image1,
                        R.drawable.mage2,
                        R.drawable.image3,
                        R.drawable.mage4};

then use this array by using index variable of and integer to get the drawable as you want. For e.g. you want to display circle slide so it start with 0 and when it reached to end it'll start from beginning

private int index = 0;

imageSwitcher.postDelayed(new Runnable() {
            public void run() {
                imageSwitcher.setImageResource(imgs[index]);
                if(index==(imgs.length-1))
                     index = 0;
                else
                     index++;
                imageSwitcher.postDelayed(this, 1000);
            }
        }, 1000);
Pratik
  • 30,639
  • 18
  • 84
  • 159