Skip to content Skip to sidebar Skip to footer

How Do I Periodically Change Background Image?

I want to change the background image of my app on a one second timer (changing the background between two images) . I know how to change the image on a button press, but I'm havin

Solution 1:

You could use View.postDelayed(Runanble r, long delayMillis). For example, something like:

public void onCreate() {
    ...
    ImageView backgroundImageView = findViewById(R.id.background);
    backgroundImageView.postDelayed(new Runnable() {
        static int i = 0;
        public void run() {
            ImageView.this.setImageResource(
                i++ % 2 == 0 ?
                    R.drawable.background_image1 :
                    R.drawable.background_image2);
            ImageView.this.postDelayed(this, 1000);
        }
    }, 1000);
}

Post a Comment for "How Do I Periodically Change Background Image?"