Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timed popup in Android

I am creating a matching game for Android, and when the user gets a match, a dialog box should pop up saying "Match!" I cannot figure out how to do this though. If I use Thread.currentthread().sleep, the dialog never appears.

android.app.AlertDialog a = new android.app.AlertDialog.Builder(match.this).setTitle("Match!").show();
Thread.currentthread().sleep(1000);
a.dismiss();

Nothing happens - the program just hangs for a second. I would like it to pop up for just 1 second, or if there is another sort of popup type thing, that would be good too.

like image 491
Isaac Waller Avatar asked Jan 17 '09 23:01

Isaac Waller


2 Answers

You're trying to show a text message in a popup for a short period of time on the screen?

For these kind of alerts toasts are great:

Toast.makeText(this, "Match!", Toast.LENGTH_LONG).show();

Is that what you are looking for? Here is the Java Doc.

like image 138
Mariano Kamp Avatar answered Sep 30 '22 18:09

Mariano Kamp


The dialog is shown in the current thread but you are putting the thread to sleep so it never shows up. Other than event throttling, there are few cases where you want to call sleep with a substantial delay from the UI thread.

In this case using a Toast is easiest as the previous poster suggested. A couple of other ways to handle work you want done in the future

  • Java Timers. The action will happen
    on a different thread so you have to be carefull what gui calls you make
  • Views have a postDelayed(Runnable action, long delayMillis) method will cause the Runnable to be executed on the UI thread after roughly delayMillis.
like image 20
hacken Avatar answered Sep 30 '22 18:09

hacken