DEV Community

loading...
Cover image for Care for a toast in the snackbar?

Care for a toast in the snackbar?

tkuenneth profile image Thomas Kuenneth ・2 min read

Since Android's first version, developers can use android.widget.Toast to show small informative messages to the user.
A simple Toast
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()
Enter fullscreen mode Exit fullscreen mode

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

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()
Enter fullscreen mode Exit fullscreen mode

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 Snackbars. 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. 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
The code is beautifully simplistic, just like Toasts.

info.text = getString(R.string.snackbar)
val snackbar = Snackbar.make(info, info.text, Snackbar.LENGTH_LONG)
snackbar.setAction(R.string.dismiss) {
}
snackbar.show()
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (0)

pic
Editor guide