3

Is there a way I can stop a toast message programmatically?

Say I have a button which I click to scroll through toast messages, and in the onclick event I wanted to stop all in the queue and just show the new one, how would I do that?

A simplified version of my code is below - Code:

public class Help extends Activity{

LinearLayout background;
int screenNo = 1;
Toast toast;

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);

       background = (LinearLayout) findViewById(R.id.helpLayout);

        ImageButton next = (ImageButton) findViewById(R.id.imageButtonNext);
        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                toast.cancel();
                showNextScreen();
            }});        
 }

private void showMessageBox(String title, String msg) {
    AlertDialog.Builder b = new AlertDialog.Builder(this);
     b.setTitle(title);
     b.setMessage(msg);
     b.setPositiveButton("Next", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {
             showNextScreen();
        }});
     b.setNegativeButton("Quit Help", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {
                 returnHome();
        }});
     b.show();
 }

private void showNextScreen() {
    int time = 7000;
    String tstMsg = "error";    

    switch (screenNo) {
    case 1:
        break;          
    case 2:
        break;          
    case 3:
        break;          
    case 4:
        break;
    case 5:
        toast.cancel();
        returnHome();
        break;

    default:
        break;
    }

    if(screenNo < 5)
    {
    toast=Toast.makeText(this, tstMsg, time);   
    toast.setGravity(Gravity.BOTTOM, 0, 0);
    toast.show();
    screenNo++;
    }
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ben Taliadoros
  • 7,003
  • 15
  • 60
  • 97
  • Isn't Toast#cancel() what you need? http://developer.android.com/reference/android/widget/Toast.html#cancel%28%29 – ernazm Dec 19 '11 at 15:24
  • Possibly a duplicate from http://stackoverflow.com/questions/2755277/android-hide-all-showed-toast-messages – Telmo Marques Dec 19 '11 at 15:25

3 Answers3

5

This is how i achieved this one.

public static Toast toastShow;

public void showToast(Activity actRef, String message) {

    if (toastShow == null
            || toastShow.getView().getWindowVisibility() != View.VISIBLE) {
        toastShow = Toast.makeText(actRef, message, Toast.LENGTH_SHORT);
        toastShow.setGravity(Gravity.CENTER, 0, 0);
        toastShow.show();
    }
}

define above code in separate class and instantiate that class where you want show message,you are done with it.

Deepak
  • 61
  • 1
  • 4
4

Create a custom global object

private Toast toast;

Initialize it in onCreate

 toast = Toast.makeText(YOUR_CLASS_NAME.this, "", Toast.LENGTH_SHORT);

Whenever you need to show a Toast

toast.setText("Hi....");
toast.show();

To kill all the message based on requirement onPause or onDestroy

toast.cancel();
Mayank Mehta
  • 689
  • 1
  • 8
  • 12
2

You're all free to cancel the Toast object.

Wroclai
  • 26,835
  • 7
  • 76
  • 67
  • Cancel doesnt do it for me, there still exists a queue – Ben Taliadoros Dec 19 '11 at 15:32
  • @Ben: Keep your `Toast` as a reference when you're calling `makeText(...)` and after that you can cancel it. The queue can only be canceled by calling `cancel()` on individual `Toast` objects. – Wroclai Dec 19 '11 at 15:35
  • I have added the code, it's been simplified. could you explain what you mean by keep as a reference? – Ben Taliadoros Dec 19 '11 at 15:43
  • @Ben: Keep track of every `Toast`s you're creating. When you want to cancel them you have to call `cancel()` on every `Toast` that you've created. – Wroclai Dec 19 '11 at 16:01
  • but as the toast is a global variable, and toast.cancel is called everytime the onclick happens, should this not be the case? – Ben Taliadoros Dec 19 '11 at 16:09
  • @Ben: I don't get your "problem". Doesn't the `Toast`s interrupt? – Wroclai Dec 19 '11 at 16:12
  • when i click the button repeatedly, they all queue and display one after the other, after i have passed the screen i want them to display on – Ben Taliadoros Dec 19 '11 at 16:19
  • @Ben: Make sure you really cancel all the `Toast`s that are active before displaying new ones, or like @TomS states - only change the text. :-) – Wroclai Dec 19 '11 at 16:21
  • Doesnt really solve my problem, Ill keep it as it is though. thanks for the help. – Ben Taliadoros Dec 19 '11 at 16:37