Care for a toast in the snackbar?
Tags: android, ux, kotlin
Published: 2020-04-17 14:26:17

Since Android's first version, developers can use `android.widget.Toast` to show small informative messages to the user.
![A simple Toast](https://dev-to-uploads.s3.amazonaws.com/i/2uh33pbb0i870g1daxdb.png)
To create and show the above message, just invoke `makeText()` and (on its result) `show()`. Toasts can be customized to some extent, for example by specifying how long the message shall be visible. 
```
Toast.makeText(this, "Hello Toast", Toast.LENGTH_LONG)
     .show()
```
`setGravity(Gravity.CENTER, 0, 0)` allows you show the toast in the middle of the screen. That is, unless your app targets Android 11. The Android R preview documentation says that in this case `setGravity()` will become no-op. The same applies to `setMargin()`. Some other methods (for example `getGravity()` or `getHorizontalMargin()`) should no longer be called if the app targets Android R, as the returned values may no longer reflect the actual value. Please consult the updated developer documentation for details.

To customize a toast even further, you can set a custom view. The following code snippet will produce a toast like this:
![A toast with a custom view](https://dev-to-uploads.s3.amazonaws.com/i/hes8i012lygwcex82poq.png)
````
val image = ImageView(this)
image.setImageDrawable(getDrawable(R.mipmap.ic_launcher_round))
val custom = Toast(this)
custom.duration = Toast.LENGTH_LONG
custom.view = image
custom.show()
```
The problem is that, starting with Android 11, custom toast views are deprecated. Although my code currently still works for apps targeting R when they are in the **foreground**, such toasts will **not** be displayed from the background. Google suggests to use plain text toasts or `Snackbar`s. We will grab a snack shortly. You can read more about custom toast views being blocked in Google's [Behavior changes: Apps targeting Android 11](https://developer.android.com/preview/behavior-changes-11#custom-toast-views-blocked). If your app needs to know when a toast appears or disappears, you can use the new `addCallback()` and `removeCallback()` methods. This applies to both text and custom view toasts.

Snackbars, like toasts, show informative messages to the user. Besides the different visual appearance, the allow a little more interaction. For example, you can add actions like this:
![A Snackbar](https://dev-to-uploads.s3.amazonaws.com/i/z624snvx0n5isd3lvuqm.png)
The code is beautifully simplistic, just like `Toast`s.
```
info.text = getString(R.string.snackbar)
val snackbar = Snackbar.make(info, info.text, Snackbar.LENGTH_LONG)
snackbar.setAction(R.string.dismiss) {
}
snackbar.show()
```
There is, however, one thing you need to be aware of... Snackbars follow the Material Design language and you must include the Material Components library in your app module's *build.gradle*:
```
implementation 'com.google.android.material:material:1.1.0'
```
So, staying with toasts or fancying snackbars is to some extent a matter of personal taste. But if you use no other component from `com.google.android.material` you should probably consider if it's worth the weight of an additional library.

You can find my sample app [here](https://github.com/tkuenneth/android-demos/tree/master/misc/ToastOrSnackbar).