In this tutorial I am going to discuss you about how to setting up a basic Snackbar, adding actions on it and Android SnackBar example.

Android SnackBar is just similar to Android Toast meassages except it provides you action button to interact with or to perform some action. Snack bar is an android support design widget which is used to show the message at the bottom of the screen.

Only one SnackBar can appear on screen at a time. Snack Bar shows the message at the bottom of the screen on mobile and lower left on larger devices like tab. The message automatically goes away after a short period.

Android SnackBar uses static make(…) method to setup/Configure snack bar in the desired way. The make(…) method takes three parameters, a View , a string message to display and a duration to display the SnackBar. They automatically disappear after a timeout or after user interaction elsewhere on the screen.

Available duration’s are:

LENGTH_INDEFINITE

LENGTH_LONG

LENGTH_SHORT

The make(…) method only creates the snackbar, for showing the snackbar use the show() method with SnackBar instance.

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒

select Empty Activity and go through it.

Read Also : Intent in Android Studio

2. Add Android design support library to build.gradle file :

Implement Android design support library inside your project build.gradle file .

implementation 'com.android.support:design:27.1.1'

Try to add the latest version of the Android design support library. Once that’s done, save the file and perform a Gradle sync.

3. Create a Button to your layout file :

Here we create a button to your layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

<Button android:id="@+id/snack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Create SnackBar"/>

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/snack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Create SnackBar"/> </RelativeLayout>

4. Implement SnackBar.make(…) method to setup/Configure snack bar :

Now implement SnackBar.make(…) method to create snackbar message to display the SnackBar we will call show() method , like toast message in MainActivity.java file or just add the following code .

Snackbar snack = Snackbar.make(view,"Hello world",Snackbar.LENGTH_INDEFINITE); snack.setAction("Close", new View.OnClickListener() { @Override public void onClick(View v) { snack.dismiss(); } }).setActionTextColor(getResources().getColor(android.R.color.holo_red_light)).show();

MainActivity.java :

package net.technxt.snackbar; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button snack; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); snack = (Button)findViewById(R.id.snack); snack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final Snackbar snack = Snackbar.make(v,"Hello world",Snackbar.LENGTH_INDEFINITE); snack.setAction("Close", new View.OnClickListener() { @Override public void onClick(View v) { snack.dismiss(); } }).setActionTextColor(getResources().getColor(android.R.color.holo_red_light)).show(); } }); } }

Now run the test app to test SnackBar popup message in android app through android device or any Emulator .

Share this: WhatsApp

Facebook

Twitter

Reddit

LinkedIn

