BY PARESH MAYANI - JULY, 1ST 2015

Welcome to the second part of the Android design support library tutorial series. In the first part, we talked about Floating action button, its attributes and some of the FAB issues.

Today we will talk about another component “Snackbar”.

Welcome Snackbar, Goodbye Toast!

“Providing lightweight, quick feedback about an operation is a perfect opportunity to use a snackbar.”

Snackbar is another component available in design support library. Using Snackbar, we can show a quick message at the bottom (mostly) of the screen. It’s almost similar to Toast message, but a bit more flexible:

  • It automatically disappears after a time out or after user interaction on the screen.
  • It can contain an action, which is optional.
  • We can dismiss Snackbar by swiping it off the screen.
  • It is a context sensitive message and so those messages are part of the UI screen and appears above all other elements on the screen, not like Toast message overlying on a screen.
  • Only one snackbar can be displayed at a time.

Snackbar mostly inherits the same methods and attributes as the Toast, example LENGTH_LONG and LENGTH_SHORT attributes for setting the duration.

 

 

基本使用用法:

 

Snackbar.make(rootlayout, "Hello SnackBar!", Snackbar.LENGTH_SHORT)
       .setAction("Undo", new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // Perform anything for the action selected
           }
       })
       .show();

  

简介:

 

Snackbar.make(view, message, duration)
       .setAction(action message, click listener)
       .show();

  

方法:

  • make() – To make a Snackbar to display a message
  • setAction() – To set an action
  • show() – To show a Snackbar

 

参考: android weekly

 

 http://www.technotalkative.com/part-2-welcome-snackbar-goodbye-toast/