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:
- flip the dealer card and diplay the dealer hand points
- 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
}
}
});