Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is Snackbar isQueued () used? (How to queue the snackbar)

Tags:

If i call the snackbar multiple times in a row, only the last snackbar item is displayed.

e.g. with the codes below, only Item 3 would be shown. it seems that Snackbar.LENGTH_LONG is ignored (and set to zero?) for item 1 and 2.

Snackbar.make(view, "Item 1", Snackbar.LENGTH_LONG).show();
Snackbar.make(view, "Item 2", Snackbar.LENGTH_LONG).show();
Snackbar.make(view, "Item 3", Snackbar.LENGTH_LONG).show();

yet in the google documents, I see that it is possible to queue the messages.

public boolean isShownOrQueued ()

Returns whether this Snackbar is currently being shown,
or is queued to be shown next.

so how do we actually queue the snackbar?

like image 233
Angel Koh Avatar asked Oct 21 '15 16:10

Angel Koh


People also ask

How do I use snackbar?

This example demonstrates how do I use snackBar in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is snackbar explain it with example?

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.

How do you set a snackbar position?

Snackbars should be placed at the bottom of a UI, in front of app content.


Video Answer


2 Answers

Here is a partial snippet that solves your problem, although it might not be the correct way to go about things:

//using a queue to pass string to the snackbar
Queue<String> myQueue = new LinkedList<String>();
myQueue.offer("item 1");
myQueue.offer("item 2");
myQueue.offer("item 3");
displaysnack(myQueue, view);

public void displaysnack(final Queue dQueue, final View view){
    Snackbar.make(view, (String)dQueue.poll(), Snackbar.LENGTH_LONG).setCallback(new Snackbar.Callback() {
        @Override
        public void onDismissed(Snackbar snackbar, int event) {
            switch (event) {
                case Snackbar.Callback.DISMISS_EVENT_ACTION:
                    Toast.makeText(getApplicationContext(), "Clicked the action", Toast.LENGTH_LONG).show();
                    break;
                //once the timeout expires, display the next one in the queue.
                case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:
                    Toast.makeText(getApplicationContext(), "Showing: "+ (dQueue.size()), Toast.LENGTH_SHORT).show();
                    if (dQueue.size()>0){displaysnack(dQueue, view);}
                    break;
                case Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE:
                    //Toast.makeText(getApplicationContext(), "Multiple Shown", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
like image 148
Anon Avatar answered Nov 28 '22 19:11

Anon


I also needed to implement a queue of snackbars but did not find ready solution. So I decided to implement it on my own. You can try it https://github.com/AntonyGolovin/FluentSnackbar.

Just call important() on Builder and it will be added to the queue.

like image 22
Anton Holovin Avatar answered Nov 28 '22 19:11

Anton Holovin