Java Show a Messagebox for a short time and make it disappear by itself

1.4k views Asked by At

after reading a lot of solutions on AlertDialog I finally managed to get a message displayed. The message is the counter for the rounds in a little game. The Message should pop up, before the player enters the next round. It should remain a few seconds and then disappear by itself.

What happens: The programm gets stuck in showing the alert window and does not go to the i++ line. Any idea how to solve this problem without introducing a button that the user must hit? I have taken out the dismiss() because there is a screenrefresh afterwards anyway. But at the moment it woudn't reach that point anyway. I am working with Android Studio 2.2.2

Would be great, if someone could look into it. Thanks in advance.

Here is the code:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Date;
import java.util.Random;

private void startRound() {
    int i=0;
    round = round +1;
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(" ");
    alertDialog.setMessage("Round " + round);

    while(i<100) {
        alertDialog.show();
        i++;
    }

//            alertDialog.dismiss();


        screenrefresh();
        handler();
5

There are 5 answers

1
Ghorbanzadeh On

You should show your dialog and use handler to dismiss :

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    alertDialog.dismiss();
  }
}, 100);

If you refresh activity you can use Toasts .

you can customize your toast style .

0
Sadiq Md Asif On

i++ is not increasing because your alert dialog needs to be closed first. so it's stuck.

Below code shows dialog and closes after 5 seconds and it can not be dismissed by the user.

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(" ");
        alertDialog.setMessage("Round " + round);
        alertDialog.setCancelable(false);  // if you want it not to be dismissed by user

        alertDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);  // seconds * 100
                } catch (InterruptedException e) {
                    if (alertDialog.isShowing())
                    {
                        alertDialog.dismiss();
                    }
                    e.printStackTrace();

                }finally {
                    if (alertDialog.isShowing())
                    {
                        alertDialog.dismiss();
                    }
                }
            }
        }).run();
0
Sergei Bubenshchikov On

For you task snackbars will be more acceptable.

Add CoordinatorLayout in layout.xml

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"       
    android:layout_height="match_parent"       
    android:layout_alignParentBottom="true"       
    android:layout_centerHorizontal="true"       
    android:id="@+id/snackbarPosition"/>

And just run snackbar:

final View.OnClickListener clickListener = new View.OnClickListener() {
    public void onClick(View v) {
       ...
    }
};

final View coordinatorLayoutView = findViewById(R.id.snackbarPosition);
Snackbar
    .make(coordinatorLayoutView, R.string.snackbar_text, Snackbar.LENGTH_LONG)
    .setAction(R.string.snackbar_action_undo, clickListener)
    .show();
0
Rissmon Suresh On

There is one more option u can trust on

Try implementing AlarmManger as the Dialog is shown to user

For AlarmManger Implementation

Please use

PendingIntent.getBroadcast(mContext,
                12345, intent, PendingIntent.FLAG_ONE_SHOT);

and u can also cancel alarm manager

0
Daffiprogs On

I have chosen the hint from Ghorbanzadeh to use the Toast. Now it looks like this:

private void startRound() {
    round = round +1;
    if( round != 1 ) {
        CharSequence text = ("Starte Runde " + round);
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(this, text, duration);
        toast.setGravity(Gravity.CENTER_HORIZONTAL |Gravity.CENTER_VERTICAL, 0, 0);
        toast.show();
    }
}

The messsage is centered in the screen and lasts long enough to be seen. This was the short and easy code that I was looking for. Thanks again for the fast help.