Is there code for Snackbars in Android L or are we expected to implement them ourselves?
Asked Answered
A

5

18

The Material design website mentions a new Toast-like element called a Snackbar: http://www.google.com/design/spec/components/snackbars-and-toasts.html

The Android L preview SDK documentation (sorry can't link since it's only downloadable) doesn't have any mention of Snackbar in the classes list or as a modifier in the Toast class documentation. Am I missing something obvious or should I build my own Snackbar.java?

Aspia answered 2/9, 2014 at 23:0 Comment(3)
Bear in mind that L is a preview; just because L does not have Snackbar does not mean that Android 5.0 (or whatever it is numbered) will not have Snackbar.Semibreve
I get that, but everything else new has code in the preview SDK, this seems to be the one thing they forgot or didn't have ready or didn't plan to add themselves. I'd just like to know if anyone found code or has implemented their own version.Aspia
Hmmm...still seems to be missing from the production sdk. I've seen blogs from other Google employees saying there might be more updates over time, but strange its not in there yet.Ballance
A
24

Update 2015-05-29:

Google released a Design Support Library which includes a Snackbar and other Material Design widgets.

The Snackbar lib mentioned in the original answer is now deprecated.

Original answer

I'm sure Google will eventually include it in a future SDK, along with a Floating Action Button that is also missing in the preview SDK.

As @friedrich nietzche pointed out, I implemented a library to include a Snackbar in your project.

https://github.com/nispok/snackbar

Hope it helps!

Adulation answered 13/9, 2014 at 21:56 Comment(1)
Snackbars support was recently added to Android Support Library revision 22.2.0: developer.android.com/reference/android/support/design/widget/…Sabayon
W
4

FWIW,

It would appear that there is no Snackbar implementation in the L Developer Preview. I've also implemented a Snackbar library with the intentions of being as close to the material design guidelines as I can. Thanks.

Wormhole answered 22/9, 2014 at 16:19 Comment(5)
It doesn't seem like it's possible to dismiss them by sliding left/right, and the animation is a bit weird.Crassulaceous
That's because I'm following the material design guidelines. You dismiss by swiping up/down not left/right and thats where the animation was taken from as well. Here's a video material-design.storage.googleapis.com/publish/v_2/…Wormhole
Well that's weird. it acts like temporary notifications, is capable of being dismissed, yet needs a different way to be dismissed ?Crassulaceous
But it's not in a list or a piece in a set - it's a singular unit.Wormhole
True, but I didn't expect that. I've now tried to show it on Gmail, and noticed the same behavior (swipe down). I consider this as not comfortable and not intuitiveCrassulaceous
A
3

Mabye take a look at this here. http://www.williammora.com/2014/08/snackbar-android-library.html

I am guessing the native version will show up in the sdk eventually. It is a bit odd I agree.

Ardra answered 7/9, 2014 at 8:49 Comment(0)
C
1

Snackbar is effectively just a Crouton with some margins. Crouton in its current form only supports adding to start (0th item) of a ViewGroup, however you can find the very "strayan" enhancement to Crouton, DownUnderMode, at my github. Just beware that the official Crouton library and DownUnderMode version are a little out of sync (which will hopefully be fixed in the year 2058 when the DownUnderMode pull request is accepted).

Chatham answered 17/3, 2015 at 6:29 Comment(0)
S
0

Here is simple way to implement snackbar in android

Step 1. Add support library 23 and compile your project with

compile 'com.android.support:appcompat-v7:23.0.1'

Step 2. Add coordinate layout in your activity file

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


</android.support.design.widget.CoordinatorLayout>

Step 3. Now add following code in your MainActivity.java to implement snackbar

public class MainActivity extends AppCompatActivity {

    CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        ShowSnack();
    }

    public void ShowSnack() {
        Snackbar snackbar = Snackbar.make(coordinatorLayout, "Snackbar Label", Snackbar.LENGTH_LONG);
        snackbar.setAction("Action", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Action", Toast.LENGTH_LONG).show();
            }
        });
        snackbar.setActionTextColor(Color.RED);
        View snackbarView = snackbar.getView();
        snackbarView.setBackgroundColor(Color.DKGRAY);
        TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        snackbar.show();
    }
}

Hope this will work for you.

For more android tutorial please follow this blog: Trinity Tuts

Shearin answered 21/10, 2015 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.