Friday, July 24, 2020

Snackbar in Android

How to use Snackbar in Android


Snackbar is used to show some information or feedback about an operation.
Toasts can also be used for the same purpose, but Snackbar privides more features and optiona than Toasts.

There are basically 3 parts of Snackbar

View : The view used to make the snackbar
Text : The message, Feedback or Info that you want to shoe
Time : How long the Snackbar is to show.

   There are 3 options for time

LENGTH_INDEFINITE (Show the snackbar until it's either dismissed or another snackbar is shown)
LENGTH_LONG (Show the snackbar for a long period of time)
LENGTH_SHORT (Show the snackbar for a short period of time)

you can see detail aboy Snackbar class and methods here at   Snackbar in Android

How to make a Snackbar

Snackbar.make(view, R.string.text_label, Snackbar.LENGTH_SHORT) .show()
How to add an action to Snackbar

Snackbar.make(view, R.string.text_label, Snackbar.LENGTH_LONG)
    .setAction(R.string.action_text) {
        // Responds to click on the action
    }
    .show()

Positioning a Snackbar

By default, Snackbars will be placed at  the bottom edge of their parent view. However, you can use the setAnchorView method to make a Snackbar appear above a specific view within your layout, e.g. a Button.

Snackbar.make(...)
    .setAnchorView(button)

1 comment:

  1. I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!.. 먹튀검증사이트

    ReplyDelete