Another interesting component introduced in Material Design is the Snackbar. 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.
This article explains about snackbar with few examples covering different scenarios.
1. Simple Snackbar
Below is the syntax of a simple snackbar. The make function accepts three parameters. View, display message and duration of the message to be displayed.
Normally passing CoordinatorLayout as view param is the best option as it allows Snackbar to enable some features like swipe-to-dismiss and automatically moving of widgets like FloatingActionButton.
And the duration should be LENGTH_SHORT, LENGTH_LONG or LENGTH_INDEFINITE. When LENGTH_INDEFINITE is used, the snackbar will be displayed indefinite time and can be dismissed with swipe off.
Snackbar snackbar = Snackbar .make(coordinatorLayout, "Welcome to CodeDecode", Snackbar.LENGTH_LONG); snackbar.show();
2. Snackbar with Action Callback
You can also mention a callback interaction method using setAction() method. This allows us to take certain action when user interacts with the snackbar.
Snackbar snackbar = Snackbar .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG) .setAction("UNDO", new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT); snackbar1.show(); } }); snackbar.show();
3. Customizing the Snackbar View
Snackbar comes with default white color text and #323232 background color. You can override these colors as mentioned below.
Snackbar snackbar = Snackbar .make(coordinatorLayout, "No internet connection!", Snackbar.LENGTH_LONG) .setAction("RETRY", new View.OnClickListener() { @Override public void onClick(View view) { } }); // Changing message text color snackbar.setActionTextColor(Color.RED); // Changing action button text color View sbView = snackbar.getView(); TextView textView = (TextView) sbView.findViewById(R.id.snackbar_text); textView.setTextColor(Color.YELLOW); snackbar.show();
4. Creating New Project
1. In Android Studio, go to File ⇒ New ⇒ New Project and fill all the details required to create a new project.
2. Open build.gradle and add design support library dependency.
build.gradle
dependencies { implementation 'com.google.android.material:material:1.1.0' }
3. Open the layout file activity_main.xml and paste below code. Here you can see I have added the CoordinatorLayout.
activity_main.xml
<androidx.coordinatorlayout.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"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp" android:paddingRight="20dp" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <Button android:id="@+id/btnSimpleSnackbar" android:background="@color/colorAccent" android:textColor="#fff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="Simple Snackbar" /> <Button android:id="@+id/btnActionCallback" android:textColor="#fff" android:background="@color/colorAccent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="With Action Callback" /> <Button android:id="@+id/btnCustomSnackbar" android:textColor="#fff" android:background="@color/colorAccent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Custom Color" /> </LinearLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
4. Now open MainActivity.java and make the changes as mentioned below. This activity contains three button click listeners to show the different ways of implementing the snackbar covering the scenarios explained above.
MainActivity.java
package com.codedecode.yashpwr; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.coordinatorlayout.widget.CoordinatorLayout; import com.google.android.material.snackbar.Snackbar; public class MainActivity extends AppCompatActivity { private CoordinatorLayout coordinatorLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); coordinatorLayout = findViewById(R.id.coordinatorLayout); Button btnSimpleSnackbar = findViewById(R.id.btnSimpleSnackbar); Button btnActionCallback = findViewById(R.id.btnActionCallback); Button btnCustomView = findViewById(R.id.btnCustomSnackbar); btnSimpleSnackbar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Welcome to CodeDecode", Snackbar.LENGTH_LONG); snackbar.show(); } }); btnActionCallback.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG) .setAction("UNDO", new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT); snackbar1.show(); } }); snackbar.show(); } }); btnCustomView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "No internet connection!", Snackbar.LENGTH_LONG) .setAction("RETRY", new View.OnClickListener() { @Override public void onClick(View view) { } }); // Changing message text color snackbar.setActionTextColor(Color.RED); // Changing action button text color View sbView = snackbar.getView(); TextView textView = (TextView) sbView.findViewById(R.id.snackbar_text); textView.setTextColor(Color.YELLOW); snackbar.show(); } }); } }
Run and test the app.
Happy Coding 🙂
Yash Pawar
Yash is hardcore programmer and programming has been his passion since he compiled his first hello-world program. Solving real problems of developers through tutorials has always been interesting part for him.