1

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?

masmic
  • 3,526
  • 11
  • 52
  • 105
  • 2
    Use Full Link for u http://learnandroideasily.blogspot.in/2013/05/customizing-display-time-of-toast.html – Naveen Tamrakar Sep 17 '13 at 08:14
  • [Related code here shows how to cancel a Toast view](http://stackoverflow.com/questions/16098151/android-cancel-toast-when-exiting-the-app-and-when-toast-is-being-shown). Unfortunately it does not show how to keep it displayed for an indeterminate amount of time. – Richard Le Mesurier Aug 16 '16 at 21:12

5 Answers5

4

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

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • Can I use the processDialog as you have used the toast to start and cancell it programmatically? I tought that the ProcessDialog was just used to show some kind of bar with the process progress. – masmic Sep 17 '13 at 08:18
  • Yes, it can be cancelled programatically and is used especially when it comes to network operations or when doing intense calculations that take time. Here is an example: http://androiddesk.wordpress.com/tag/progress-dialog-with-an-example/ – Philipp Jahoda Sep 17 '13 at 08:28
  • `set the Toast duration to like 30 Seconds` [not possible at all](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/com/android/server/NotificationManagerService.java#NotificationManagerService.scheduleTimeoutLocked%28com.android.server.NotificationManagerService.ToastRecord%2Cboolean%29) `YERY_LONG_TOAST_TIME` is irrelevant ... it always will be long = 3,5 sec. and short 2 sec. – Selvin Sep 17 '13 at 08:33
  • You are right. Sorry for that misstake. I will edit that thank you. – Philipp Jahoda Sep 17 '13 at 08:35
  • @Philipp Jahoda I'm trying to set the progressDialog as a better option for this case. The only problem wich I'm getting is to set a (r.string.wait) in it's parameters instead of setting the sentence on it, for future language compatibilities. But it tells that this method (context, charsequence, charsequence, booblean) is not applicable for (context, string, string, boolean) – masmic Sep 17 '13 at 08:43
1

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
silvia_aut
  • 1,481
  • 6
  • 19
  • 33
  • Thanks @silvia_aut, but I'm not trying to set a toast shorter or longer than the default. I'm just trying to know how to show some kind of message that I can start and cancel programmatically – masmic Sep 17 '13 at 08:11
  • Then make a normal toast to a variable like above and then you can show it or cancel it if you need it. – silvia_aut Sep 17 '13 at 08:15
0
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); 
dipali
  • 10,966
  • 5
  • 25
  • 51
0

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.

J Jiang
  • 1,452
  • 10
  • 14
-1

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.

Leonidos
  • 10,482
  • 2
  • 28
  • 37