Snackbar


Source link: https://github.com/nispok/snackbar

DEPRECATED

This lib is deprecated in favor of Google's Design Support Library which includes a Snackbar and is no longer being developed.

Thanks for all your support!

Snackbar

Library that implements Snackbars from Google's Material Design documentation. Works on API levels >= 8

Installation

You can import the library from source as a module or grab via Gradle:

compile 'com.nispok:snackbar:2.11.+'

Usage

Using the Snackbar class is easy, this is how you would display it on an Activity:

Snackbar.with(getApplicationContext()) // context
  .text("Single-line snackbar") // text to display
  .show(this);
 // activity where it is displayed

However, I recommend you use the SnackbarManager to handle the Snackbars queue:

// Dismisses the Snackbar being shown, if any, and displays the new one SnackbarManager.show(
  Snackbar.with(myActivity)
  .text("Single-line snackbar"));

If you are using getApplicationContext() as the Context to create the Snackbar then you must specify the target Activity when calling the SnackbarManager:

// Dismisses the Snackbar being shown, if any, and displays the new one SnackbarManager.show(
  Snackbar.with(getApplicationContext())
  .text("Single-line snackbar"), myActivity);

You can place the Snackbar at the bottom of a particular hierarchy of views. The sample app makes use of this; check out SnackbarImmersiveModeSampleActivity:

SnackbarManager.show(Snackbar snackbar, ViewGroup parent) {
 
}
 SnackbarManager.show(Snackbar snackbar, ViewGroup parent, boolean usePhoneLayout) {
 
}

If you want an action button to be displayed, just assign a label and an ActionClickListener:

SnackbarManager.show(
  Snackbar.with(getApplicationContext()) // context

.text("Item deleted") // text to display

.actionLabel("Undo") // action button label

.actionListener(new ActionClickListener() {

 @Override

 public void onActionClicked(Snackbar snackbar) {

  Log.d(TAG, "Undoing something");

 
}

}
) // action button's ActionClickListener

, this);
 // activity where it is displayed

If you need to know when the Snackbar is shown or dismissed, assign a EventListener to it. This is useful if you need to move other objects while the Snackbar is displayed. For instance, you can move a Floating Action Button up while the Snackbar is on screen. Note that if you only need to override a subset of the interface methods you can extend from EventListenerAdapter:

SnackbarManager.show(
  Snackbar.with(getApplicationContext()) // context

.text("This will do something when dismissed") // text to display

.eventListener(new EventListener() {

 @Override

 public void onShow(Snackbar snackbar) {

  myFloatingActionButton.moveUp(snackbar.getHeight());

 
}

 @Override

 public void onShowByReplace(Snackbar snackbar) {

  Log.i(TAG, String.format("Snackbar will show by replace. Width: %d Height: %d Offset: %d",

  snackbar.getWidth(), snackbar.getHeight(),

  snackbar.getOffset()));

 
}

 @Override

 public void onShown(Snackbar snackbar) {

  Log.i(TAG, String.format("Snackbar shown. Width: %d Height: %d Offset: %d",

 snackbar.getWidth(), snackbar.getHeight(),

 snackbar.getOffset()));

 
}

 @Override

 public void onDismiss(Snackbar snackbar) {

  myFloatingActionButton.moveDown(snackbar.getHeight());

 
}

 @Override

 public void onDismissByReplace(Snackbar snackbar) {

  Log.i(TAG, String.format(

"Snackbar will dismiss by replace. Width: %d Height: %d Offset: %d",

snackbar.getWidth(), snackbar.getHeight(),

snackbar.getOffset()));

 
}

 @Override

 public void onDismissed(Snackbar snackbar) {

  Log.i(TAG, String.format("Snackbar dismissed. Width: %d Height: %d Offset: %d",

 snackbar.getWidth(), snackbar.getHeight(),

 snackbar.getOffset()));

 
}

}
) // Snackbar's EventListener
  , this);
 // activity where it is displayed

There are two Snackbar types: single-line (default) and multi-line (2 lines max. Note this only applies for phones; tablets are always single-line). You can also set the duration of the Snackbar similar to a Toast.

The lengths of a Snackbar duration are:

  • LENGTH_SHORT: 2s
  • LENGTH_LONG: 3.5s (default)
  • LENGTH_INDEFINTE: Indefinite; ideal for persistent errors

You could also set a custom duration.

Animation disabling is also possible.

SnackbarManager.show(
  Snackbar.with(getApplicationContext()) // context

.type(Snackbar.SnackbarType.MULTI_LINE) // Set is as a multi-line snackbar

.text("This is a multi-line snackbar. Keep in mind that snackbars are " +

 "meant for VERY short messages") // text to be displayed

.duration(Snackbar.SnackbarDuration.LENGTH_SHORT) // make it shorter

.animation(false) // don't animate it
  , this);
 // where it is displayed

You can also change the Snackbar's colors and fonts.

SnackbarManager.show(
  Snackbar.with(getApplicationContext()) // context

.text("Different colors this time") // text to be displayed

.textColor(Color.GREEN) // change the text color

.textTypeface(myTypeface) // change the text font

.color(Color.BLUE) // change the background color

.actionLabel("Action") // action button label

.actionColor(Color.RED) // action button label color

.actionLabelTypeface(myTypeface) // change the action button font

.actionListener(new ActionClickListener() {

 @Override

 public void onActionClicked(Snackbar snackbar) {

  Log.d(TAG, "Doing something");

 
}

 
}
) // action button's ActionClickListener
  , this);
 // activity where it is displayed

Finally, you can attach the Snackbar to a AbsListView (ListView, GridView) or a RecyclerView.

SnackbarManager.show(
  Snackbar.with(getApplicationContext()) // context

.type(Snackbar.SnackbarType.MULTI_LINE) // Set is as a multi-line snackbar

.text(R.string.message) // text to be displayed

.duration(Snackbar.SnackbarDuration.LENGTH_LONG)

.animation(false) // don't animate it

.attachToAbsListView(listView) // Attach to ListView - attachToRecyclerView() is for RecyclerViews

, this);
 // where it is displayed

It uses Roman Nurik's SwipeToDismiss sample code to implement the swipe-to-dismiss functionality. This is enabled by default. You can disable this if you don't want this functionality:

NOTE: This has no effect on apps running on APIs < 11; swiping will always be disabled in those cases

SnackbarManager.show(
  Snackbar.with(SnackbarSampleActivity.this) // context

.text("Can't swipe this") // text to be displayed

.swipeToDismiss(false) // disable swipe-to-dismiss functionality
  , this);
 // activity where it is displayed

Examples

There's a sample app included in the project. SnackbarSampleActivity is where you want to start.

Apps Using Snackbar

Contributing

If you would like to add features or report any bugs, open a PR or refer to the issues section.

Contributors

Thanks to all contributors!

License

MIT

ChangeLog

Go to the releases section for a brief description of each release.

Resources

SquiDB is a SQLite database layer for Android. It is designed to make it as easy as possible to work with SQLite databases while still enabling the power and flexibility of raw SQL. SquiDB combines features of an ORM with object-oriented SQL statement builders to make it easy to read and write your data without a bunch of messy SQL strings. It also includes built in tools and hooks to help you easily write database migrations as well as implement ContentProviders.

Fast & simple benchmarking framework for Android. Ever had the feeling that certain parts of your code were slow? Now you can prove it (and make sure your refactor worked).

Pretty material design toasts with feedback animations.

A Material Design ViewPager easy to use library.

Android Intent wrapper with fluid API and argument validations.

Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platform the code is running.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes