views:

2461

answers:

4

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.

+8  A: 

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.

Mariano Kamp
Perfect! Thank you very much!
Isaac Waller
You're welcome ;-)
Mariano Kamp
i also did this and got it but is there any method to display it at the center of application or another location?
RBADS
+1  A: 

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.
hacken
A: 

Though Toast is good idea it may not serve for some situation the best idea is to use popup window in such cases, though popup window looks tricky its way too easy to implement i also recently wanted to implement it after an hour of scratching head i made it work http://www.ceveni.com/2009/09/popup-window-in-android-sample-program.html and the best place to search for this implementation is android source code calendar module in class "calendarview" and method "updateEventDetails"

hope this gave you a hint!

A: 

@another coder

thanx it worked

Manjunath