Search code examples
androidloopstimerimageswitcher

Android Imageswitcher: switch images periodically?


I am using an ImageSwitcher with a TouchListener to change images from an array. Its working fine but i want it to switch images every x seconds or so, so that I can add imageSwitcher.setImageResource(imageList[curIndex]); to it.

Any suggestions?


Solution

  • Try this,

     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);