Use Of Material Design Snackbar In Android Apps

Snackbar provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

Snackbar api do not have any public or default constructor to create an instance directly, instead you have to create the instance by calling make method. This make method requires three parameters i.e., root view, message to display, duration of the message to display.

If you are using CoordinatorLayout in your layout, you have to pass the CoordinatorLayout as a root view. CoordinatorLayout  is the best option as it allows Snackbar to enable some features like swipe-to-dismiss and automatically moving of widgets like FloatingActionButton.

Basic Snackbar Example

snackbar

Here is the sample code :

Snackbar.make(coordinatorLayout, "Onetouchcode welcome's you", Snackbar.LENGTH_LONG).show();

If your layout do not have CoordinatorLayout then also you can use , the sample code is :

Snackbar.make(findViewById(android.R.id.content), "Onetouchcode welcome's you", Snackbar.LENGTH_LONG).show();

Customization of Snackbar

snackbar

Snackbar comes with default text color and background based on your defined theme. But most of the time we need to change it to make it more aligned with our apps. To do such customization you have to change the background and text color in the following way –

final Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "Onetouchcode welcome's you", Snackbar.LENGTH_LONG);
        View snackbarView = snackbar.getView();
        snackbarView.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
        TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.WHITE);
        snackbar.show();

 Snackbar with action callback

snackbar

Snackbar is very handy and is little similar to Toast but additionally you can add actions too into it. Adding actions with callback is simple to do, here is the sample code for it –

final Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "Onetouchcode welcome's you", Snackbar.LENGTH_LONG);
        snackbar.setAction("Close", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                snackbar.dismiss();
            }
        });
        snackbar.setActionTextColor(Color.BLUE);

        View snackbarView = snackbar.getView();
        snackbarView.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
        TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.WHITE);
        snackbar.show();

These are the different variants you can use in your apps. But you have to know few additional things before adding this funky widget in your Android apps, these are as such –

  1. If you call Snackbar from an activity and then if activity get closed, unlike Toast this widget will not be visible at all.
  2. It is recommended to show one Snackbar at a time.
  3. If you do not use CoordinatorLayout your Snackbar may overlap on FloatActionButton if any available in your layout.
  4. It is recommended to add maximum of two actions per Snackbar.
  5. To know more check the documentation.

 

I hope this article will help you all to implement Snackbar in your Apps. If you like this article do not forget to share it with your friends and colleagues.

Happy coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *