0

I want to display an other image after a delay. Here is my code:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView=findViewById(R.id.imageView);

    ArrayList<String> test = new ArrayList<String>();
    test.add("e");
    test.add("a");
    if (test.get(0) == "e") {
        Glide.with(this)
                .load("https://something.something/something.jpg")
                .into(imageView);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if (test.get(1) == "a") {
        Glide.with(this)
                .load("https://something.something/something2.jpg")
                .into(imageView);
    }
}
}

But only the 2nd image apppears if i do that. Any solution?

Guillaume Raymond
  • 1,726
  • 1
  • 20
  • 33
Pokekman
  • 3
  • 3

1 Answers1

0

Create a new method

private void loadImage(String imageLink){
    Glide.with(this).load(imageLink).into(imageView);
}

now simply call this where you want to load first image and second image in the same imageView

loadImage("imageLink");

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //Do something after 3sec
        loadImage("next ImageLink");
    }
}, 3000);

Something like this, you can also modify loadImage method so it can also accept ImageView as a parameter if you want to load images in different ImageView with delay

if (test.get(0) == "e") {
    loadImage("https://something.something/something.jpg");
}
if (test.get(1) == "a") {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 3sec (3000 = 3 sec)
            loadImage("https://something.something/something2.jpg");
        }
    }, 3000);
}

Hope this will help!

Edit 1:

if you want to load both pics with delay use

if (test.get(0) == "e") {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 3sec (3000 = 3 sec)
            loadImage("https://something.something/something.jpg"); // image 1 
        }
    }, 3000);
}
if (test.get(1) == "a") {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 3sec (3000 = 3 sec)
            loadImage("https://something.something/something2.jpg"); // image 2
        }
    }, 3000);
}
Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
  • Welcome, please accept my answer and also [read this](http://stackoverflow.com/help/how-to-ask) . your question lacked some information. – Rahul Gaur Jan 08 '20 at 10:21
  • I want to ask something, how do I add delay to the first condition too? – Pokekman Jan 10 '20 at 12:48
  • add `Handler` to the first `if` also – Rahul Gaur Jan 11 '20 at 03:40
  • check updated answer, and starting thinking and doing stuff yourself, also please at least try and if you are trying, **try harder** . if you tried same code but it is not working tell me – Rahul Gaur Jan 11 '20 at 11:37
  • That is exactly what i did. But still only the 2nd picture shows up. It works if i change the delay of the first picture to lower than 3 seconds, but if i do that, the array cant work dynamically – Pokekman Jan 11 '20 at 14:04
  • sorry, you have to create 1 handler and call `postDelayed()` inside it, also you need to check `if` statement inside it, [take a look here](https://stackoverflow.com/questions/42379301/how-to-use-postdelayed-correctly-in-android-studio) to learn `postDelayed()` – Rahul Gaur Jan 13 '20 at 03:34
  • tried the one on the link, the problem still occurs. Also what do you mean by 'check the if statement inside it'? – Pokekman Jan 14 '20 at 14:08
  • set `picture_url` and call `handler`. show image inside `handler` and at the bottom of handler update `picture_url` and call `postDelayed` so it will call the handler again. add some checker so it will not become `infinite loop` – Rahul Gaur Jan 15 '20 at 03:39