Connect with us

Hi, what are you looking for?


Example of Android Snackbar

Example of Android Snackbar

In this article we will learn more about the implementation of different forms of Android snack bar widgets in our application. It is an interesting part of the material design library. The Android snack bar replaces toast and offers actions for interaction. This blog explains the Snackbar with some examples.

What is the Android Snackbar

The Android Snackbar has been used to display comments on certain edits and to make toasts. A message is displayed at the bottom of the screen. The snack bar appears on the unit’s screen and is displayed for a specified period of time. They disappear automatically after a time-out or after interaction with the user. The nibbler can be removed so that it can be detached.

Android SnackbarUse case

Here are some examples of how to use Snackbar for Android

  • Regular cafeteria
  • A snack with action

1. Normal snack

The following piece of code is an example of Snackbar in Android.

Snackbar.make(@NonNull CharSequence text,@NonNull int duration) ;

Snackbar.make(@NonNull CharSequence text,@NonNull int duration) ;

Snackbar.make(@NonNull CharSequence text,@NonNull int duration) ;

The snackbar class has a static method make() which accepts three parameters. First, including View (he used to find a suitable ancestor to display the Snackbar).

The second is a string or resource identifier (Text to be displayed can be formatted as text ) and is the third of one of these maturities (Duration of message display This can be LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE or a modified duration in milliseconds.). Each parameter is annotated with @NoNull, which means it should not be null and void.

Restaurant behaviour is better with a DinerLayout.  They allow the Snackbar to activate behaviors like fill and automatically move widgets like the FloatingActionButton.

Actual Snecbarversion

This is a real implementation of the Snecbar.

// find the ID of the coordination plan/

See contextView = findViewById( ;

// Make and show me an appetizer…

Snackbar.make(contextView, example snackbar for Android, Snackbar.LENGTH_SHORT)

// coordinator findLayout id view contextView = findViewById( ; // make(contextView, Android Snackbar example, Snackbar.LENGTH_SHORT) .show() ;

// coordinator findLayout idAnsicht contextView = findViewById(;// make(contextView, Android Snackbar example, Snackbar.LENGTH_SHORT).show() ;

2. Snack with action

The Snackbar allows us to perform certain actions when a user is interacting with the Snackbar, which allows us to perform actions when a user is interacting with the Snackbar action button.

// find the ID of the coordination plan/

See contextView = findViewById( ;

// Make and show me an appetizer…

Snackbar.make(contextView, Internet connection loss, Snackbar.LENGTH_SHORT) ;

// Setting the action with the repeater

snackbar.setAction(Retry, new TryAgainListener() ;

// Find coordinatorLayout id View contextView = findViewById(; // Create and show Snackbar Snackbar = Snackbar.make(contextView, Internet connection lost, Snackbar.LENGTH_SHORT); // Snackbar snackbar.setAction(retry, new TryAgainListener()); // Show the snackbar snackbar() ;

// find coordinatorLayout id
View contextView = findViewById( ;
// do and show Snackbar
Snackbar Snackbar =
Snackbar.make(contextView, Internet connection lost, Snackbar.LENGTH_SHORT) ;
// set action with Retry Listener
snackbar.setAction(Retry, new TryAgainListener() ) ;

// show snackbar ;

Try listening again

This listener manages the event onClick() by pressing the Repeat button.

the TryAgainListener public class implements View.OnClickListener {.

click on public blank (display v) {

// Code to undo the last user action

See contextView = findViewById( ;

// Make and show me an appetizer…

Snackbar.make(contextView, internet connection, Snackbar.LENGTH_SHORT)

public class TryAgainListener implemented View.OnClickListener { @ overwrite public space onClick(View v) { // Code to view the last user action View contextView = findViewById(; // Snackbar Snackbar.make(contextView, connected to the Internet, Snackbar.LENGTH_SHORT) .show(); } . }

the TryAgainListener public class implements View.OnClickListener {.

publicly void onClick(View v) {
// Code to undo last user action
View contextView = findViewById(;
// Make(contextView, Internet connected, Snackbar.LENGTH_SHORT)
publicly void onClick(View v).

3. Customizing your Android snack bar

The Android Snack Bar does not allow you to specify a custom layout. But it follows the manufacturer’s design, so it’s easy to wash. Let’s take a look at the picture below.

Example of Android Snackbar

Hardware development

Snackbar offers building methods to customize the color of the action text, as shown below

Action style text color

snackbar.setActionTextColor (colorId)

snackbar.setActionTextColor (colorId)

snackbar.setActionTextColor (colorId)

Text colour, font, font size, number of lines, etc., as follows

The methods of snack building do not offer such things directly. To do this, we must first obtain View.

View snackbarView = snackbar.getView() ;

View snackbarView = snackbar.getView() ;

View snackbarView = snackbar.getView() ;

Now get TextView from View and apply all TextView properties as shown below.

Display textView text = snackbarView.findViewById( ;

textView.setTextColor(colorId) ;

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22) ;

Display textView text = snackbarView.findViewById(; // Set the number of text lines textView.setMaxLines(2); // Set the text color textView.setTextColor(colorId); // Set the text size textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);

TextView textView = snackbarView.findViewById(;
// defines the absence of the text string
textView.setMaxLines(2) ;
//s sets the text color in
textView.setTextColor(colorId) ;
//s sets the text size in
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22) ;

Background color

snackbarView.setBackgroundColor(colorId) ;

snackbarView.setBackgroundColor(colorId) ;

snackbarView.setBackgroundColor(colorId) ;

Example of an Android snack bar application

The conceptual part is over, we move on to the actual execution. Open the Android Studio project and create a new project. You know that Android redesigns and combines all design and support libraries into two libraries: androidx and material io.

1. Open the .gradle application Add hardware dependencies

Introduction of

Introduction of

Introduction of

2. Add color to the source file we will use in this demo application


#1976D2>/ Color>




> Sources> #2196F3>Color> #1976D2>Color> #1976D2>Color>. #1976D2>Colour> #1976D2>Colour> #1976D2>Colour>#1976D2>Colour> #1976D2>Colour> #1976D2>Colour> #1976D2>Colour>ColorPrimaryDark color=colorAccent>#FFEB3B #E64A19 #FBE9E7


3. I’ve made some style changes.



4. Open activity_main.xml and add the followingcomponent

In this presentation I have added a number of buttons to show the different implementations of the snack bar.




android: layout_width=match_parent

android: layout_height=match_parent


android: [email protected]+id/text display

android: layout_width=wrap_content

android: layout_height=wrap_content

android: layout_marginBottom=8dp

android: text=Android Snackbar Example




app:layout_constraintTop_toTopOf= higher


android: [email protected]+id/buttonEasy

android: text=Normal snacks



app:[email protected]+id/textView

android:[email protected]+id/buttonAction

android:text=Snack bar with action button



app:[email protected]+id/buttonEasy

android: [email protected]+id/User defined view button

android: text=Snackable with custom display



app:[email protected]+id/buttonAction

[email protected]+id/textView android:layout_width=wrap_content android:layout_height=wrap_content android:layout_marginBottom=8dp android:text=Android Snackbar Example android:textSize=24sp app:layout_constraintBottomOf=parent app:layout_constraintLeft_toLeftOf=parent app:layout_constraintRightOf=parent app:layout_constraintTop_toTopOf=parent app:layout_constraintVertical_bias=0.243 /> [email protected]+id/ buttonEasy android:text=normal Snackbar app:layout_constraintEnd_toEndOf=parent app:layout_constraintStartOf=parent app :[email protected]+id/textView [email protected]/btnStyle />

You May Also Like