As you must know that toast message can last for only a SHORT and LONG duration, but if you want to hide a toast message. But what if an event causes the toast messages to pile up and get displayed one after the other cause a bad user experience, so in such situation, it is better to cancel the pending messages queue, here is how we can do it,
Simple toast messages example that shows toast 10 times in a loop!
Toast myToast = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i=0;i<=10;i++) {
myToast = Toast.makeText(MainActivity.this, "Toast message: "+i,Toast.LENGTH_LONG);
myToast.show();
}
}
How to cancel or hide Toast messages
Button cancelToastButton = (Button) findViewById(R.id.cancelToastButton);
cancelToastButton.setOnClickListener(new View.OnClickListener() {
if (myToast != null || myToast.getView().getWindowVisibility() == View.VISIBLE) {
myToast.cancel();
}
});
The above code when the button is pressed it should cancel/hide/flush all pending toast messages that are getting displayed.
More Posts related to Android,
- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
More Posts:
- How to check your installed version of Git - Git
- How to install Packages in Sublime Text Editor - Sublime-Text
- Transfer files between Android and Mac OS X using usb cable - Mac-OS-X
- Locate MainActivity Java or Kotlin file in Android Studio - Android-Studio
- HTML, HTML5. CSS, CSS3 Color Codes 2020 - Html
- How to run a Maven jar project from eclipse without tomcat - Eclipse
- Docker Desktop needs privileged access macOS - MacOS
- 10 Beginners Commands for macOS Terminal Usage - MacOS