Ticker

6/recent/ticker-posts

How to use Snackbar in Android

How to use Snackbar in Android

snackbar in android


The Snackbar is a view that provides brief feedback about app processes at the bottom of the mobile screen. It appears above all elements on the screen, indicating the user to perform that operation first. It supports content labeling for approachability and is understandable by most screen readers.

Snackbar is just like the Toast message but in case of snackbar user can interact with it. It can offer actions like "OKAY", "DISMISS", "RETRY", "UNDO", "REDO", etc. It automatically disappears after a timeout or after user interaction.  

Note: Snackbars work properly if they are held inside of a CoordinatorLayout. CoordinatorLayout allows the snackbar to enable performance like swipe-to-dismiss, as well as automatically moving widgets like FloatingActionButton


Snackbar vs. Toast
  • Snackbar appears above all elements on the screen, unlike toast that may block any significant part of the screen.
  • Snackbar performs actions like undo, redo, retry, etc. But toast not. 
  • Users can easily dismiss the snackbar, but in the case of toast, users need to wait until the end of the time scheduled for it.  

Showing the snackbar 
To create a snackbar, calling make creates a snackbar. Creating and showing are two different things. To show a snackbar, use the show method on the return Snackbar instance. 

To show snackbar with a message and no action: 
  • The view is used to make a snackbar. 
  • Generally, it viewed that interacted with a trigger. 
  • Let's say the trigger is a button. 
To show snackbar with a message and no action



Adding an action
To add action, use the setAction method on that object return from make. 
Sanckbar is automatically closed when the action is performed. 

Snackbar with action:

Snackbar with action



Anchoring the snackbar 
By default, the snackbar is anchored at the bottom of their parent's view. But we can use setAnchorView method to appear the snackbar above a specific widget within that layout. For example, a FloatingActionButton.

Anchoring the snackbar



Using a snackbar in Android Studio 
To create a snackbar in the android studio, you just need a library. For that, we used a Material Design Library. 
Add the library dependency into our build.gradle (app) file: 


As we are using material design, so we need to change some styling of the layout. For that go to res/values/style.xml file and change the theme to Theme.MaterialComponents.Light.NoActionBar




Creating the snackbar in the android studio (XML file)

first, create the view of the snackbar. And also add a Floating ActionButton to show how the Anchoring things worked.

activity_main.xml



Creating the snackbar in the android studio (Java file)
Open the main activity and add the below code. 
Here we make the snackbar, set the timeout, set the anchor view, set the action, set the animation, and then display the snackbar on the screen 

MainActivity.java



Output:

Quick Recap:

  • The Snackbar is a view that provides brief feedback about app processes at the bottom of the mobile screen.
  • There are many advantages of using snackbar over toast.
  • make method is used to create a snackbar and show method is used to display that snackbar.
  • setAction is used to set an action on the snackbar.
  • setAnchorView method is used to display the snackbar over any view.





Post a Comment

0 Comments