Sunday, 9 April 2017

Android: Snackbar example

Snackbar is one of the most interesting component introduced in Material Design. Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen and can be swiped off in order to dismiss them.
Snackbars 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.
They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity.
Snackbars can contain an action which is set via setAction(CharSequence, View.OnClickListener).

Also to be notified when a snackbar has been shown or dismissed, you can provide a Snackbar.Callback via addCallback(BaseCallback).


Implementing Snackbar is very simple which can be done as follows:

Snackbar snackbar = Snackbar
        .make(coordinatorLayout, "Snackbar is visible.", Snackbar.LENGTH_LONG)
        .setAction("DONE", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Snackbar dismissed.", Snackbar.LENGTH_SHORT);
                snackbar1.show();
            }
        });

snackbar.show();


One point to be noted here is that, while making a Snackbar I have used the CoordinatorLayout as the parent layout for Snackbar. Due to this CoordinatorLayout our Snackbar has the ability to be dismissed when swiped.


Tha's all.
Share and comment if any issues.


No comments:

Post a Comment