-1

I'm doing a blackjack game using android studio and I encountered a problem when running the sleep method. I want that when the player hit the stand button it will do 2 tasks:

  1. flip the dealer card and diplay the dealer hand points
  2. keep dealing cards to the dealer until he has 17. and having a dealy between every card creation and diplaying the new value of points every card creation. and I want to have a delay between the 2 tasks. but when I use the sleep method it stops for the duration and then it is displaying the 2 tasks instantly.

code:

stand.setOnClickListener(new View.OnClickListener() { 
                    
            @Override
            public void onClick(View view) {
          //task 1
                ImageView iv;                                         // declaritaion          
                int i = 0;

                iv = new ImageView(MainActivity.this);                // make the imageview for the card i want to flip
                 
                iv.setTranslationX(400);                              // set all attributes
                iv.setTranslationY(1000);
                iv.setLayoutParams(layoutParams);

                String s = r.getdHand().getQ().get(0).getImg();       // get the image name of the card I want to flip (I already have the card stored in dealer hand)
                int imagekey = getResources().getIdentifier(s, "drawable", getPackageName());
                iv.setImageResource(imagekey);
                dealerPoints.setText(Integer.toString(r.getdHand().sum)); // change the text view that displays the dealer points

                main.addView(iv);                                        // add card
                                // here i want a delay of 1 second
                 // task 2
                while (r.getdHand().sum < 17) {
                    ImageView add;                                                       // declariation
                    int index = 2;

                    cards c = r.d.dealNextCard();                                        // add card to dealer hand
                    r.getdHand().add(c);

                    add = new ImageView(MainActivity.this);                         // make a new image view
                    add.setTranslationX(400 + 190 * index);
                    add.setTranslationY(1000);
                    add.setLayoutParams(layoutParams);

                    s = r.getdHand().getQ().get(index).getImg();                   // get the image name
                    imagekey = getResources().getIdentifier(s, "drawable", getPackageName());
                    add.setImageResource(imagekey);
                    main.addView(add);
                    dealerPoints.setText(Integer.toString(r.getdHand().sum));      // change the dealer points in the text view as before

                    index++;
                              // here i want another delay between every card creation
                }
            }
        });
mmm
  • 1
  • I haven't done Android in awhile, but if it's similar to swing, then you're doing everything on the main event dispatch thread. Typically, code in listeners should occur in a separate thread. Try wrapping your code in a Runnable() and then executing it that way. – Ryan Feb 07 '22 at 22:41
  • If you want to repeat a sequence of statements after some delay use `Handler.postDelayed` - example: https://stackoverflow.com/a/42379610/17856705. You cannot "sleep" on the ui thread or two things occur: ui freezes or ANR. In the handler simply repeat the same postDelayed for recurrence. `onClick` is on the UI thread. – Computable Feb 07 '22 at 23:25

1 Answers1

0

You can use this to sleep for a certain period of time

TimeUnit.SECONDS.sleep(1)

Put it where you want the delay

AlexM28
  • 71
  • 2