Question

In my app, I've got a procedure that can last between 2 and 15 seconds more or less. What I want is to set a kind of toast that when the procedure starts shows:

Loading values. Wait...

Just now, I've setted the toast duration to SHORT, because if the procedure lasts about 5 or less seconds, a LONG will be just that, very long. But having setted the duration to SHORT, when it lasts more than 10 seconds the toast dissapears and there is no message showing that the app is still processing so the user can start touching things.

What I want is to set something like a toast but that I can programmatically cancel when the procedure is finished. Any ideas?

Was it helpful?

Solution

I would recommend that you simply set the Toast duration to the maximum possible time and then use the Toast object returned from Toast.makeText(...) to cancel it when your process is finished.

Toast t = Toast.makeText(....., YERY_LONG_TOAST_TIME);
t.show();

public void onYourTaskFinished() {
    t.cancel();
}

Something like that. I personally would recommend using a ProgressDialog btw: http://developer.android.com/reference/android/app/ProgressDialog.html

OTHER TIPS

Here's an example:

final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);
toast.show();

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
       @Override
       public void run() {
           toast.cancel(); 
       }
}, 1000); //specify delay here that is shorter than Toast.LENGTH_SHORT
final Toast toast = Toast.makeText(ctx, "hello android", Toast.LENGTH_SHORT);

Handler h= new Handler();
    h.postDelayed(new Runnable() {
       @Override
       public void run() {
           toast.show(); 
       }
}, 1000); 



    h.postDelayed(new Runnable() {
       @Override
       public void run() {
           toast.cancel(); 
       }
}, 3000); 

You can have a handler of the Toast when you create it, pass it to you job and call the show() method when the job starts and call the cancel() method when the job finish.

However, from your description, toast message might not be your best choice. Toast is more like a hint which has little impact if the user misses it. With little background infomation about your app, I think you might need a ProgressDialog if you do not want the user to touch anything while you are loading data, or a ProgressBar if you just want the user to know your job's progress.

Do not use Toast for this purpose. You should use progress dialog or you can add progress indicator to notification bar. So user will be able to see progress even not being inside your app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top