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?

Tim Trueman
  • 1,513
  • 3
  • 17
  • 28
  • 1
    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`. – CommonsWare Sep 02 '14 at 23:37
  • 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. – Tim Trueman Sep 03 '14 at 17:36
  • 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. – John Shelley Oct 31 '14 at 01:17

5 Answers5

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!

wmora
  • 1,203
  • 1
  • 9
  • 17
  • 4
    Snackbars support was recently added to Android Support Library revision 22.2.0: https://developer.android.com/reference/android/support/design/widget/Snackbar.html – ljmelgui May 28 '15 at 20:39
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.

MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • It doesn't seem like it's possible to dismiss them by sliding left/right, and the animation is a bit weird. – android developer Nov 19 '14 at 08:25
  • 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 http://material-design.storage.googleapis.com/publish/v_2/material_ext_publish/0B0NGgBg38lWWZFcwa2wyTG1ybE0/components-snackbarstoasts-spec-061302_Mobile_Snackbar_xhdpi_002.webm – MrEngineer13 Nov 19 '14 at 15:40
  • Well that's weird. it acts like temporary notifications, is capable of being dismissed, yet needs a different way to be dismissed ? – android developer Nov 19 '14 at 15:55
  • But it's not in a list or a piece in a set - it's a singular unit. – MrEngineer13 Nov 19 '14 at 15:57
  • 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 intuitive – android developer Nov 19 '14 at 17:32
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.

samst
  • 536
  • 7
  • 19
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).

straya
  • 5,002
  • 1
  • 28
  • 35
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

Aneh Thakur
  • 1,072
  • 12
  • 20