0

Hello I'm trying to delay imageviews to give a perception that they are being taken out one by one. I've tried

Thread.Sleep

CountDownTimer

Runnable/Handler

But they either only delay once and both imageviews change at the same time or do not delay at all. For some reference I'm trying to do something like

 private void delaycard(final int Card) {

        newcard(Card); //Delay this before it is called
    }

Willing to try/retry anything at this point

Community
  • 1
  • 1
Seabass77
  • 187
  • 5
  • 13
  • I'm confused as to why handler wouldn't work. What're you trying to do with it? – Matter Cat Jul 07 '15 at 01:46
  • The handler works but only delays the first imageview. Both imageviews show after the the handler is run. without a delay inbtween them. – Seabass77 Jul 07 '15 at 02:50

2 Answers2

1

Off the top of my head, try something like this:

                    Handler h = new Handler();
                    //for the number of images we have
                    for (int i = 0; i < numImages; i++) {

                        //we create a runnable for that action
                        Runnable r = new Runnable() {
                            @Override
                            public void run() {
                                newCard(card);
                            }
                        };

                        //this is the amount of time to delay it by
                        delay = delay + 500;
                        //effectively, we're creating a series of runnables at the same time
                        //but they activate one after the other in .5s intervals
                        h.postDelayed(r, delay);
                    }
Matter Cat
  • 1,538
  • 1
  • 14
  • 23
  • This worked except that I didn't need a for loop, what was happening is that I wasn't making more than 1 runnable. Can you explain some of this code as I want to learn how to do it in the future. – Seabass77 Jul 07 '15 at 03:13
  • 1
    A handler is a sort of listener on your main thread that you can insert commands (runnables) into, which you do via `h.postDelayed(r, delay)`. By incrementing `delay` value, what you're saying is basically, "I have x runnables, now run them at these points in time" (.5, 1, 1.5, etc). It's a bit of an illusion in that you actually give out all your orders at the same time; they just appear to run at different times because of the delay. – Matter Cat Jul 07 '15 at 03:30
0

put this inside your method

     Handler handler = new Handler();
     handler.postDelayed(new Runnable() {
      @Override
       public void run() {
           newCard(Card);
       }
     }, 100);
Sheychan
  • 2,415
  • 14
  • 32
  • When I run the code delaycard(21); delaycard(22); both imageviews (from newCard(Card) show at the same time. The first one is delayed but the second one isn't. – Seabass77 Jul 07 '15 at 02:46
  • Owww so it's many, then what you need is thread pool use nostra13/universalimageloader for displaying images instead, it's very useful and it also has many other features. Happy coding. https://github.com/nostra13/Android-Universal-Image-Loader – Sheychan Jul 07 '15 at 02:55