Reputation: 3031
I have toast in method showText(); This method shows toast when we call that method. In second Activity I have a button and when I click on the button my Toast must be shows. Everything work great but when I click two or mnore times my toast will be showing long time. I want to toast show only when I click on button and when I click again, first toast disapear and shows again.
public void showText(String msg) {
Toast.makeText(this, msg, 1000).show();
}
How I can do this?
Upvotes: 0
Views: 206
Reputation: 7635
you can do it this way
class YourActivity extends Activity implements OnclickListener
{
Toast toast = null;
void onclick(View v)
{
//call showText() method
}
// modify your showText as follows
public void showText(String msg) {
if(toast != null)
{
toast.cancel();
toast = null;
}
toast = new Toast(YourActivity.this);
toast.setText(msg);
toast.show()
}
}
Upvotes: 1
Reputation: 1462
Instead of calling show(), you can keep a reference to the Toast you just created
Toast toast = Toast.makeText(this, msg, 1000);
then toast.show();
and then later, call some methods on the toast like toast.cancel();
http://developer.android.com/reference/android/widget/Toast.html
Upvotes: 3