SnackBar is a feature that allows the user to get some message from the application. It is similar to the well known Toast feature for showing messages to the user. SnackBar is distinguished from the Toast in a way that SnackBar can be placed only on the bottom on the screen, popping up from bottom, with solid color for background. Also, the most interesting part for the SnackBar is that it can have fully functional button for some kind of action.This example will be divided in to three part tutorials, regarding of the java version and usage of ButterKnife library.

In all cases we need to add the android design support library and synchronize the project so the library can be initialized.

And here is the layout for the project, containing three different buttons for three different actions of the SnackBar ,depending of the usage:

We will start first with the plain plain (and most common) Android pattern with Java 7 of building things:

The ViewGroup object is our layout that we use in our activity. We can use ViewGroup in every activity, because every layout extends from this class. Ofcourse we add id tag on our root layout. The root layout can be every layout that we want, LinearLayout. RelativeLayout and we can declare this layouts like RelativeLayout rootLayout; LinearLayout rootLayout, and so on. ViewGroup bind all of the layouts and like this the margin of error is minimal, we dont need to track which layout we use.
The layout is declared globaly so we can access it in multiple scopes of methods, but it is instantiated in our @OnCreate overriden method.

This code below is the same thing as above, but using the ButterKnife library:

In @OnCreate method, we are setting up the buttons using the plain Android pattern with Java 7:

and set to them onClickListeners later.

Simple SnackBar implementation:

On the SnackBar object we call its static make() method that takes parameters for View layout, String message and int duration. We call here our rootLayout, add appropriate message, and set the duration, again static constant that SnackBar have .LENGTH_ that can be LENGTH_SHORT, LENGTH_LONG and LENGTH_INDEFINITE. At the end of the statement we must call static show() method, otherwise the snackbar wont be shown on the display.

Simple SnackBar with ButterKnife implementation:

 

 

SnackBar with Button implementation plain Android with Java 7:

The difference between the first and the second one is the .setAction() method that add`s button functionality. the setAction() takes CharSequence or simply String, for the text of the button and onClickListener() for when the button is clicked. The onClickListener works the same way as on any button in Android. After that we must call show() method.

SnackBar with Button using ButterKnife implementation:

SnackBar with Button using Java 8, Lambdas & ButterKnife implementation:

 

 

 

Custom SnackBar with Button implementation plain Android with Java 7:

 

Here we have almost everithing silimilar as previous examples. Here we will set colors of the button text and message text. For the button is straight forward, using setActionTextColor(int) that receives int argument for color. You can use getColor(R.color.yellow) also.
Changing the color for the message text is a little bit different. First we must get the view for the snackbar using .getView on our previously created snackbar assigning to our View. On our view we must find the view for the text by id. That …id.snackbar_text is default for the message. On our view we set the color we want, as any other view, by calling setTextColor(int)

Custom SnackBar using ButterKnife implementation:

Custom SnackBar using Java 8, Lambdas & ButterKnife implementation:

You can get the whole project on GitHub /MaterialDesign-SnackBar

Spread the love

Leave a Reply

Your email address will not be published.