Android hide dialog after set time, like a custom time-interval toast

11,517

Solution 1

You just need to time calls to Dialog#dismiss(); for your problem Handler class would suffice(+1 to Javanator :)).

FYI, there are other classes namely AlarmManager, Timer & TimerTask that can help with timing the runs of your code.

EDIT:

Change your code to:

static DialogInterface dialog = null;
public void toast(String text, int duration) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);

    LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
    ((TextView)layout.findViewById(R.id.message)).setText(text);

    builder
        .setView(layout);


    //builder.show(); commented this line
// I don't understand the purpose of this if block, anyways
// let it remain as is at the moment
    if (dialog!=null){
        dialog.cancel();
        dialog.dismiss();
    }

    dialog = builder.create().show();


    Handler handler = null;
    handler = new Handler(); 
    handler.postDelayed(new Runnable(){ 
         public void run(){
             dialog.cancel();
             dialog.dismiss();
         }
    }, 500);


}

Solution 2

 // Make your Main UIWorker Thread to execute this statement
 Handler mHandler = new Handler(); 

Do something like this where ever your code need to dismiss the dialog.

 // this will dismiss the dialog after 2 Sec. set as per you 
 mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {     
        dialog.dismiss();
    }
 },2000L); 

Hope This Help :)

Share:
11,517
Nate Radebaugh
Author by

Nate Radebaugh

I am a Senior Consultant at SWC Technology Partners. My views and posts are my own.

Updated on June 09, 2022

Comments

  • Nate Radebaugh
    Nate Radebaugh almost 2 years

    I'm trying to display some text on the screen for a set period of time, similar to a toast, but with the ability to specify the exact time it is on the screen. I'm thinking an alert dialog may work for this, but I can't seem to figure out how to dismiss the dialog automatically.

    Can you suggest an alternative to toast notifications in which I can specify the exact time it is displayed?

    Thank you!

    static DialogInterface dialog = null;
    public void toast(String text, int duration) {
    
        final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);
    
        LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
        ((TextView)layout.findViewById(R.id.message)).setText(text);
    
        builder
            .setView(layout);
    
    
        builder.show();
    
        if (dialog!=null){
            dialog.cancel();
            dialog.dismiss();
        }
    
        dialog = builder.create();
    
    
        Handler handler = null;
        handler = new Handler(); 
        handler.postDelayed(new Runnable(){ 
             public void run(){
                 dialog.cancel();
                 dialog.dismiss();
             }
        }, 500);
    
    
    }
    
  • Rohit Sharma
    Rohit Sharma about 13 years
    AlarmManager, Timer & TimerTask are a good option too I agree +1
  • Nate Radebaugh
    Nate Radebaugh about 13 years
    This doesn't seem to work the way I expect it to. I've edited my original post with code.
  • Samuh
    Samuh about 13 years
    builder.show(); creates a new AlertDialog and shows it on the screen. You dont cache reference returned by show() so, you do not have a handle to the dialog. You then create another AlertDialog by calling create() and cache its reference. But you never show this dialog on screen. So, calling dismiss on this cached reference will have no bearing.
  • Nate Radebaugh
    Nate Radebaugh about 13 years
    That worked! Thanks so much. Sorry for the delay, I've been busy with school work and haven't had a chance to test it out.
  • Mick
    Mick over 11 years
    It is worth noting also that your activity could be paused or stopped during the delay, for example because some other event cause another activity to launch which might remove the dialog. If this happens you will get an exception when the delay is over and your handler is invoked because it is trying to dismiss a dialog which no longer exists. You can either cancel the callback if the activity is paused/stopped or add a check in the handler to see if the dialog still exists.