Toasts have been available for developers to use in their Android apps for a long time. The simplest form of pop-up message, they are unobtrusive and short-lived, wrap around their content and display at the bottom of the screen.
You may choose a Snackbar over a Toast if you need an action alongside the message or to ensure the app displays the message in the same UI where the action occurred.
The sample project contains a Copy list button, which saves the list of fruits with their quantities to the clipboard on the device. You can show a simple Toast to confirm to the user that the app successfully saved the content.
In the project, open MainActivity.kt. The button with ID button_copy already has a click listener, which calls saveToClipboard(). This method handles saving the text to the clipboard, then calls showToast(). Here, you’ll add code to display the Toast.
Add the following code:
Toast.makeText(this, R.string.toast_copied_to_clipboard, Toast.LENGTH_LONG).show()
This creates a Toast with the provided string to display and how long to show it. Build and run. Tap Copy list, and you’ll see the Toast appear at the bottom of the screen.