Timed popup in Android

Question!

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.



Answers

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


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.



You can ask the user to calibrate the system, before use. It's not fully an answer to the question, but a sollution to the problem.

Just draw a red dot in the center of the screen, top left and bottem right. Ask the user to click on them, and retrieve the coordinates. Then, you know where the corners of the screen are.

By : Gerrit


This video can help you solving your question :)
By: admin