Skip to content Skip to sidebar Skip to footer

How To Add Wait Time?

The backgrounds of all the pictures are changing at the same time. I have to change one by one, wait 5 seconds after each picture, change the other, ImageView[] imajlar={img1,img2,

Solution 1:

Use Handler for this. I hope this helps you.

new Handler().postDelayed(new Runnable(){
       @Override
       public void run() {
           changeImage();
       }
}, 5000);

Solution 2:

Try Timer with Handler

Timertimer=newTimer();
            timer.scheduleAtFixedRate(newTimerTask() {
                @Overridepublicvoidrun() {

                   //Your code
                }
            }, 0, 5000);

Solution 3:

Try this code..

newHandler().postDelayed(newRunnable() {
    @Overridepublicvoidrun() {
        // define your code..
    }
},5000);

Solution 4:

Do it like

Define an array of images.

String img[]={img1,img2,img3};

and a variable

int i=0;

now in your method

changeImage()
{
if(i<img.length())
{
//Pseudo code(your logic of setting image)
setImage.array[i];
i++;
}
else
{
//This is to start again;
i=0
}
}

Then in you defined handler.

final Handler mHandler=newHandler();
ha.postDelayed(newRunnable() {

    @Overridepublicvoidrun() {
       changeImage();

        mHandler.postDelayed(this, 5000);
    }
}, 5000);

Solution 5:

inttotalImageSize= yourTotalImageCount;

        finalHandlermHandler=newHandler();
        mHandler.postDelayed(newRunnable() {

            @Overridepublicvoidrun() {
                changeYourImage(totalImageSize);// this will give the position of image to add/remove
                totalImageSize - 1;
                if(totalImageSize > 0)
                mHandler.postDelayed(this, 5000);
            }
        }, 5000);
    }

Post a Comment for "How To Add Wait Time?"