Blog Kotlin

[Android & Kotlin] Implement Notifications and Actions in the Snackbar

The Snackbar can notify customers of straightforward messages as well as Toast. Furthermore, additionally it is potential to current an motion with button faucet with a message.

Android Studio 3.1.four
API 28
Kotlin 1.2.61

Snackbar

The snackbar shows and disappears for a brief time period, just like Toast. There are instances where it’s fascinating to go away them off routinely. The Snackbar could be custom-made until it discontinues the show interval.

Show of Snackbar

Snackbar is just like Toast, but takes View as an argument. By the means, Toast was Context.

You additionally need the Snackbar help library, so for gradle, set the dependency of com.android.help:design :.

Please regulate the version accordingly.

By the method, how is this View introduced, however show by button faucet as a method. On this case, Button has View! Because it, so it may be used.

Aside from that, for example, if you wish to display immediately with onCreate (), you’ll be able to add an id to the format file and deliver it as a View.
activity_main.xml

MainActivity.kt

Here is the code to easily show the Snackbar,
Simplified utilizing Kotlin Android Extensions.

MainActivity.kt

It’s a format.
activity_main.xml

The model is correlated with different libraries and wants adjustment. (Sometimes, this time is just suitable with this model)
build.gradle

The Snackbar is displayed.

snackbar 01- [Android & Kotlin] Implement Notifications and Actions in the Snackbar

Customise the Snackbar

Let's customize akin to setting an motion on the snackbar or altering the textual content colour.
With setAction (), message is about on the right aspect of the bar and tapping it should execute the motion. It can be used to feel like a Button.

I tried the following as a customization.

  • LENGTH_INDEFINITE: Display until the dismiss () is known as
  • setAction: Change TextView by tapping like button
  • view.setBackgroundColor (): Background shade of Snackbar
  • Snackbar textual content colour, resizing
  • Change the background shade and shade of the Action button

MainActivity.kt

activity_main.xml

strings.xml

construct.gradle

Pattern video

The textual content and faucet of setAction may be set as TextView and Button, so it may be modified in numerous methods.

Reference:
Snackbar | Android Developers