Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set timeout Dialog in android?

Tags:

android

dialog

I want to set timeout for Dialog (progress dialog) in android , to make the dialog disappears after a period of time (if there is No response for some action !)

like image 979
Adham Avatar asked Jan 25 '11 22:01

Adham


People also ask

How to use ProgressDialog in Android?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

What is setCanceledOnTouchOutside in Android?

void. setCanceledOnTouchOutside(boolean cancel) Sets whether this dialog is canceled when touched outside the window's bounds. void.

Is ProgressDialog deprecated?

Yes, ProgressDialog is deprecated but Dialog isn't.


2 Answers

The same approach as in this post is verified to work (with long instead of float):

public void timerDelayRemoveDialog(long time, final Dialog d){
    new Handler().postDelayed(new Runnable() {
        public void run() {                
            d.dismiss();         
        }
    }, time); 
}
like image 136
dave.c Avatar answered Oct 12 '22 03:10

dave.c


You could always make a class called ProgressDialogWithTimeout and override the functionality of the show method to return a ProgressDialog and set a timer to do what you wish when that timer goes off. Example:

private static Timer mTimer = new Timer();
private static ProgressDialog dialog;

public ProgressDialogWithTimeout(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public ProgressDialogWithTimeout(Context context, int theme) {
    super(context, theme);
    // TODO Auto-generated constructor stub
}

public static ProgressDialog show (Context context, CharSequence title, CharSequence message)
{
    MyTask task = new MyTask();
            // Run task after 10 seconds
    mTimer.schedule(task, 0, 10000);

    dialog = ProgressDialog.show(context, title, message);
    return dialog;
}

static class MyTask extends TimerTask {

    public void run() {
        // Do what you wish here with the dialog
        if (dialog != null)
        {
            dialog.cancel();
        }
    }
}

Then you would call this in your code as so:

ProgressDialog progressDialog = ProgressDialogWithTimeout.show(this, "", "Loading...");
like image 34
whatupman Avatar answered Oct 12 '22 02:10

whatupman