Android Snackbar Example

Android-Snackbar-Example

26th September, 2022

Android Snackbar is introduced by Android Material Design. Snackbar is light-weight widget to show messages. This section covers a little introduction to Android Snackbar.

Step 1: Create New Project

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from the templates.

Open your app level build.gradle and enable View Binding.

Project ⇒ app ⇒ build.gradle

apply plugin: 'com.android.application'

android {
    viewBinding {
        enabled = true
    }
    /*other stuff*/
}

Step 2: Add dependency

Add Material design dependency to your app level build.gradle.

Project ⇒ app ⇒ build.gradle

dependencies 
{
    implementation 'com.google.android.material:material:1.2.0-alpha05'
}

Step 3: Modify Activity

Now open the activity and modify it with the code below.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/rootLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="www.codestringz.com" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_marginTop="25dp">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Simple Snackbar"
            android:layout_marginTop="15dp"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Snackbar with Action"
            android:layout_marginTop="15dp"/>

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Custom Snackbar"
            android:layout_marginTop="15dp"/>

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Custom Snackbar 2"
            android:layout_marginTop="15dp"/>
    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
package com.codestringz.snackbarexample;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.codestringz.snackbarexample.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        init();
    }

    private void init()
    {
        binding.button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Snackbar snackbar1 = Snackbar.make(binding.rootLayout, "Simple Snackbar", Snackbar.LENGTH_SHORT);
                snackbar1.show();
            }
        });

        binding.button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Snackbar snackbar2 = Snackbar.make(binding.rootLayout, "Confirm delete?", Snackbar.LENGTH_LONG);
                snackbar2.setAction("YES", new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Snackbar snackbar21 = Snackbar.make(binding.rootLayout, "Message successfully deleted.", Snackbar.LENGTH_SHORT);
                        snackbar21.show();
                    }
                });

                snackbar2.show();
            }
        });

        binding.button3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Snackbar snackbar3 = Snackbar.make(binding.rootLayout, "No connection!", Snackbar.LENGTH_LONG);
                snackbar3.setAction("RETRY", new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Snackbar snackbar31 = Snackbar.make(binding.rootLayout, "Connecting...", Snackbar.LENGTH_SHORT);
                        snackbar31.show();
                    }
                });

                //Change action text color
                snackbar3.setActionTextColor(Color.GREEN);

                View snackbar3View = snackbar3.getView();
                TextView msgTextView = snackbar3View.findViewById(com.google.android.material.R.id.snackbar_text);
                //Change message text color
                msgTextView.setTextColor(Color.YELLOW);

                snackbar3.show();
            }
        });

        binding.button4.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Snackbar snackbar4 = Snackbar.make(binding.rootLayout, "Confirm booking?", Snackbar.LENGTH_LONG);
                snackbar4.setAction("CONFIRM", new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Snackbar snackbar41 = Snackbar.make(binding.rootLayout, "Booking confirmed", Snackbar.LENGTH_SHORT);
                        snackbar41.show();
                    }
                });

                //Change action text color
                snackbar4.setActionTextColor(Color.GREEN);

                View snackbar4View = snackbar4.getView();

                //Change message text color
                TextView msgTextView = snackbar4View.findViewById(com.google.android.material.R.id.snackbar_text);

                //Add an icon
                msgTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car_travel, 0, 0, 0);
                msgTextView.setCompoundDrawablePadding(15);

                snackbar4.show();
            }
        });
    }
}

Happy coding!

Leave a Reply