SnackbarBuilder


Source link: https://github.com/andrewlord1990/SnackbarBuilder

SnackbarBuilder

The Android Design Support library introduced the Snackbar. SnackbarBuilder provides a builder pattern that not only makes Snackbars easier to create, but it also provides some extra customisations.

One of the main annoyances with the Snackbar is that it has a dark background, but takes the default text color from your theme, which is often dark as well. This makes the messages hard to read and requires you to retrieve the TextView yourself and change the text color. SnackbarBuilder defaults the text color to white and then allows you to choose your own colour if you wish.

Download

Download via Gradle:

compile 'com.github.andrewlord1990:snackbarbuilder:0.9.0'

The library is available on Bintray (JCenter).

repositories {

jcenter() 
}

Main Features

  • A builder pattern to create Snackbars.
  • Customise message and action text color with global theme attributes and on a per-Snackbar basis.
  • Dismiss callbacks with separate methods for each type of dismiss event.
  • Theme attribute for the default view ID in an activity to attach Snackbars to, so you don't need to provide the parent view each time you wish to show one.
  • Set the default duration to use through a theme attribute.
  • Append text, which allows you to have multiple text colours.
  • Add an icon to the Snackbar if you wish.
  • A ToastBuilder to create Toast messages, with a builder pattern and global theme attributes.

Usage

Create

It is really easy to build and customise snackbars.

Snackbar snackbar = new SnackbarBuilder(this)

 .message("message")

 .actionText("Action")

 .snackbarCallback(new MySnackbarCallback())

 .build()

 .show();

Check out the sample project to see examples of how the library can be used.

Callback

Simply implement the interface for the callback you are interested in and call the appropriate method on the SnackbarBuilder. It is as simple as that!

  • Shown = showCallback(SnackbarShowCallback callback)
  • Dismissed = dismissCallback(SnackbarDismissCallback callback)
  • Dismissed (action pressed) = actionDismissCallback(SnackbarActionDismissCallback callback)
  • Dismissed (swiped away) = swipeDismissCallback(SnackbarSwipeDismissCallback callback)
  • Dismissed (timeout) = timeoutDismissCallback(SnackbarTimeoutDismissCallback callback)
  • Dismissed (manual dismiss call) = manualDismissCallback(SnackbarManualDismissCallback callback)
  • Dismissed (another Snackbar shown) = consecutiveDismissCallback(SnackbarConsecutiveDismissCallback callback)
  new SnackbarBuilder(this)

 .message("Message")

 .actionText("Action")

 .showCallback(new MyShowCallback())

 .actionDismissCallback(new MyActionDismissCallback())

 .timeoutDismissCallback(new MyTimeoutDismissCallback())

 .build()

 .show();

Alternatively, if you would rather handle all the callbacks with a single class, then you can do that too. Simply extend the SnackbarCallback class and override only the methods you are interested in. Rather than needing to check the dismissEvent integer in the Snackbar.Callback class, there is a separate method to override for each dismiss type. There is also a method that accepts the standard Snackbar.Callback.

ToastBuilder

Makes it very easy to create and customise Toast messages.

new ToastBuilder(this)
  .message("message")
  .messageTextColor(Color.BLUE)
  .duration(Toast.LENGTH_LONG)
  .gravity(Gravity.TOP)
  .build()
  .show();

The builder allows you to change the text color of the displayed message. If you wish to change the background you can provide a custom view for the Toast. By providing the ID of a TextView within this view, you can set the message on it through the builder method message(String).

Theme

You can provide defaults to the SnackbarBuilder and the ToastBuilder through theme attributes.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="colorAccent">@color/colorAccent</item>

<item name="snackbarBuilderStyle">@style/SampleSnackbarStyle</item>
  <item name="toastBuilderStyle">@style/SampleToastStyle</item> </style>  <style name="SampleSnackbarStyle">
  <item name="snackbarBuilder_parentViewId">@id/coordinator</item>
  <item name="snackbarBuilder_duration">shortTime</item>
  <item name="snackbarBuilder_messageTextColor">@color/messageText</item>
  <item name="snackbarBuilder_actionTextColor">@color/colorAccent</item>
  <item name="snackbarBuilder_iconMargin">8dp</item>
  <item name="snackbarBuilder_actionAllCaps">false</item> </style>  <style name="SampleToastStyle">
  <item name="toastBuilder_messageTextColor">@color/snackbarbuilder_default_message</item>
  <item name="toastBuilder_duration">shortTime</item> </style>

There are some base SnackbarBuilder and ToastBuilder styles included in the library. You can use these as a parent style if you would like to. These styles can be useful to se all the theme attributes you have access to.

<style name="SampleSnackbarStyle" parent="SnackbarBuilder">
  ... </style>  <style name="SampleToastStyle" parent="ToastBuilder">
  ... </style>

Suggestions

If there is any features that have been missed that you are interested in then please open an Issue.

License

Copyright 2015 Andrew Lord  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

A menu of FloatingActionButton items, designed to be anchored on an AppBarLayout.

This is powerful popup android library.

This library gives some ribbon effects on your views.

SmartAd is an easy-to-use library for AdMob and Audience Network advertising frameworks on iOS and Android.

Android app to demonstrate MVP Android Architecture using Dagger and SOLID principles.

ActivityBuilder is a annotation base library using builder pattern to make inner activity communication more easier.

Through ActivityBuilder you can use one line of code to deliver parameters, start Activity and use lambda expression to replace overriding onActivityResult.

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