Refresh the text of a Toast without expecting it to add up

2

I am creating a test application and am using toasts to validate my tests. I usually define a toast as follows:

Toast.makeText(this, "Texto que eu quero que apareça",Toast.LENGTH_SHORT).show();

Is there any way to change the text of a toast while it is still being displayed?

When I try to simply send another toast to be displayed, the first toast needs to disappear before the other one appears, I would like to know how to change it. >     

asked by anonymous 04.05.2014 / 19:30

1 answer

4

According to the reference: link .

The static method makeText returns the Toast Object created, I believe that if you save the reference to the object and use the setText method in time it is still visible, the text changes. If it does not work, you can cancel (instantially) Toast with the cancel method, and generate a new one with the new text.

Code sample:

public class MyActivity extends Activity {
    Toast mToast;

    public void showToast(String text) {
        if (mToast == null) {
            mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
        }
        mToast.setText(text);
        mToast.cancel();
        mToast.show();
    }
}
    
04.05.2014 / 19:41