Connect with us

Hi, what are you looking for?

Latest

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(android.R.id.content) ;

// Make and show me an appetizer…

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

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

// coordinator findLayout idAnsicht contextView = findViewById(android.R.id.content);// 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(android.R.id.content) ;

// 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(android.R.id.content); // 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(android.R.id.content) ;
// 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
snackbar.show() ;

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(android.R.id.content) ;

// 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(android.R.id.content); // Snackbar Snackbar.make(contextView, connected to the Internet, Snackbar.LENGTH_SHORT) .show(); } . }

the TryAgainListener public class implements View.OnClickListener {.

@Search
publicly void onClick(View v) {
// Code to undo last user action
View contextView = findViewById(android.R.id.content);
// Make(contextView, Internet connected, Snackbar.LENGTH_SHORT)
.show();
}
@Search
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(R.id.snackbar_text) ;

textView.setTextColor(colorId) ;

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22) ;

Display textView text = snackbarView.findViewById(R.id.snackbar_text); // 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(R.id.snackbar_text);
// 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 com.google.android.material:1.0.0.

Introduction of com.google.android.material:1.0.0.

Introduction of com.google.android.material:1.0.0.

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

<?xml Version=1.0 Encryption=utf-8? >

<Color name=Color/Priminal>#2196F3>Color>

<ColorPrimaryDark>#1976D2>/ Color>

<Color name=color accent>#FFEB3B</color>

<ColorName=ActionTextColor>####E64A19</Color>

<Color name=ColorBg>#FBE9E7</Color>

<?xml version=1.0 encoding=utf-8 ?> > Sources> <ColorPrimaryName=ColorPrimaryDark>#2196F3>Color> <ColorPrimaryDark>#1976D2>Color> <ColorPrimaryName=ColorPrimaryDark>#1976D2>Color>. <ColorPrimaryDark>#1976D2>Colour> <ColorPrimaryDark>#1976D2>Colour> <ColorPrimaryDark>#1976D2>Colour>#1976D2>Colour> <ColorPrimaryDark>#1976D2>Colour> <ColorPrimaryDark>#1976D2>Colour> <ColorPrimaryDark>#1976D2>Colour>ColorPrimaryDark color=colorAccent>#FFEB3B</color> <Color name=actionTextColor>#E64A19</color> <Color name=colorBg>#FBE9E7</color> </resources>

<?xml version=1.0 encoding=utf-8 ?>
<Media>
<ColorPrimary>#2196F3</Color>
<ColorPrimaryDark>#1976D2</Color>
<ColorName=ColorAccent>#FFEB3B>Color>.
<ColorName=ActionTextColor>#E64A19>Color>
<ColorName=ColorBg>#FBE9E7<Color>
</Sources>

3. I’ve made some style changes.

<style name=btnStyle parent=Text Appearance.AppCompat.Widget.button >

<name_element=android:textColor>#fff</element>

<name_element=android:background>@color/colorPrimaryDark</element>

<element name=android: layout_margin>16dp</item>

<element=android: filling>16dp</element>

<element name=android: layout_width>240dp</item>

<position_name=android:position_height>envelope_content</article>

<name_element=android:textAllCaps>false</item>.

<style name=btnStyle parent=appearance of text.AppCompat.widget.Button > <Element name=android:textColor>####fff</article> <Element name=android:background>@color/colorPrimaryDark</article> <Element name=android:layout_margin>16dp</article> <Element name=android :fill>16dp</item> <itemname=android:layout_width>240dp</item> <itemname=android:layout_height>wrap_content</item> <itemname=android:textAllCaps>false</item> </style>.

<style name=btnStyle parent=appearance of text.AppCompat.widget.Button >
<element name=android: textColor>####fff</article>
<element name=android: background>@color/colorPrimaryDark</article>
<element name=android: layout_margin>16dp</article>
<element name=android :fill>16dp</item>
<element_name=android:layout_width>240dp</item>
<element_name=android:layout_height>wrap_content</item>
<element_name=android:textAllCaps>false</item>
</style>.

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.

<?xml Version=1.0 Encryption=utf-8? >

<androidx.constraintlayout.widget.ConstraintLayout

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

tools:context=.mainActivity

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_constraintBottom_toBottomOf=parent

app:layout_constraintLeft_toLeftOf=parent

app:layout_constraintRight_toRightOf=parent

app:layout_constraintTop_toTopOf= higher

app:layout_restrictionVertical_preload=0,243

android: [email protected]+id/buttonEasy

android: text=Normal snacks

app:layout_constraintEnd_toEndOf=higher

app:layout_constraintStartOf=higher

app:[email protected]+id/textView

android:[email protected]+id/buttonAction

android:text=Snack bar with action button

app:layout_constraintEnd_toEndOf=higher

app:layout_constraintStartOf=higher

app:[email protected]+id/buttonEasy

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

android: text=Snackable with custom display

app:layout_constraintEnd_toEndOf=higher

app:layout_constraintStartOf=higher

app:[email protected]+id/buttonAction

</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version=1.0 encoding=utf-8? > <androidx.constraintLayout.widget.ConstraintLayout 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 tools:context=.Main activity > <TextView android:[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 /> <Android button:[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 /> <button android:[email protected]+id/buttonAction android:text=Snackbar with action button app:layout_constraintEnd_toEndOf=parent app :layout_constraintStart_toStartOf=parent app:[email protected]+id/buttonSimple [email protected]/btnStyle /> <Button android:[email protected]+id/buttonCustomView android:text=reload CustomView panel with CustomView :layout_constraintEnd_toEndOf=parent app:layout_constraintStartOf=parent app:[email protected]+id/buttonAction [email protected]/btnStyle /> </androidx.ConstraintLayout.widget.constraintLayout>.

<?xml version=1.0 encoding=utf-8? >
<androidx.constraintlayout.widget.ConstraintLayout
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
tools:context=.MainActivity
>.

<TextViewandroid:[email protected]+id/textViewandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_marginBottom=8dpandroid:text=Android Snackbar Exampleandroid:textSize=24spapp :layout_constraintBottomOf=higherapp:layout_constraintLeft_toLeftOf=higherapp:layout_constraintRight_toRightOf=higherapp:layout_constraintTop_toTopOf=higherapp:layout_constraintVertical_bias=0.243/><Buttonandroid:[email protected]+id/buttonSimpleandroid:text= Snackbarapp:layout_constraintEnd_toEndOf=higherapp:layout_constraintStart_toStartOf=higherapp :[email protected]+id/[email protected]/btnStyle/><keyandroid:[email protected]+id/buttonActionandroid:text=Power Panel with action keyapp:layout_constraintEnd_toEndOf=pairtapp:layout_constraintStart_toStartOf=pairtapp :[email protected]+id/[email protected]/btnStyle/><Buttonandroid: [email protected]+id/buttonCustomViewandroid: text=Custom View CustomViewandroid: text=Custom View Buyer app :layout_constraintEnd_toEndOf=parent app:layout_constraintStartOf=parent app:[email protected]+id/[email protected]/btnStyle/>android.

</androidx.constraintlayout.widget.ConstraintLayout>

5. Go to the Java source file and add the code under

Package com.example.snackbar ;

Import the android.os package;

Input android.util.TypedValue

import android.view

import android.widget.button ;

Import android.widget.textView ;

Import androidx.appcompat.appCompatActivity ;

import androidx.core.content.ContextCompatible

Import com.google.android.material.snack bar

the MainActivity public class expands AppCompatActivity {

protected hole onCreate(Bundle storedInstanceState) {

create super.onCreate(savedInstanceState) ;

setContentView(R.layout.activity_main) ;

The button simpleSnackbar = findViewById(R.id.buttonSimple) ;

ActionButtonSnackbar = findViewById(R.id.buttonAction) ;

CustomViewSnackbar button = findViewById(R.id.buttonCustomView) ;

// A simple fragment of the snack code //

simpleSnackbar.setOnClickListener(new view.OnClickListener() {)

@Define an audience blank image {.

// Make and show me an appetizer…

Snackbar.make(getView(), monster snackbar for Android, Snackbar.LENGTH_SHORT)

actionButtonSnackbar.setOnClickListener(new view.OnClickListener() {)

@Define an audience blank image {.

// Make and show me an appetizer…

Snackbar Snackbar = Snackbar.make(getView(), the message is deleted, Snackbar.LENGTH_SHORT) ;

// Press the ADD repeat button.

snackbar.setAction(Undo, new UndoListener()) ;

customViewSnackbar.setOnClickListener(new view.OnClickListener() {)

@Define an audience blank image {.

// Make and show me an appetizer…

Snackbar Snackbar = Snackbar.make(getView(), text style, font and color of the action,

snackbar.setActionTextColor(

ContextCompat.getColor(getBaseContext(), R.color.actionTextcolor)

snackbar.setAction(Undo, new UndoListener()) ;

View snackbarView = snackbar.getView() ;

Display textView text = snackbarView.findViewById(R.id.snackbar_text) ;

textView.setTextColor(ContextKompat.getColor(getBaseContext(), R.color.actionTextColor)) ;

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18) ;

Adjust /Snecbar background color/

snackbarView.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.Color.ColorBg) ;

// Identifying the click listener

The closed class UndoListener implements View.OnClickListener {.

click on public blank (display v) {

// Code to undo the last user action

See contextView = findViewById(android.R.id.content) ;

// Make and show me an appetizer…

Snackbar.make(contextView, retrieve messages, Snackbar.LENGTH_SHORT)

* Return a copy of View

* Go back to the coordinator *

to return to findViewById(android.R.id.content)

com.example.snackbar package; import android.os.bundle; import android.util.TypedValue; import android.view.view; import android.widget.button; import android.widget.textView; import androidx.appcompat.appCompatActivity; import androidx.core.content.ContextCompat; import com.google.android.material.snackbar; the MainActivity public class extends AppCompatActivity { @Defining a protected gap onCreate(bundle savedInstanceState) { super.onCreate(savedInstanceState)); setContentView(R.layout.activity_main); simpleSnackbar button = findViewById(R.id.buttonSimple); actionButtonSnackbar button = findViewById(R.id.buttonAction); customViewSnackbar button = findViewById(R.id.buttonAction).buttonCustomView); // Simple Snackbar code snippetSnackbar.setOnClickListener(new View.OnClickListener() { @Overwrite public space onClick(view) { // Snackbar Snackbar.make(getView())), Android Snackbar example, Snackbar.LENGTH_SHORT) .show(); }. }); actionButtonSnackbar.setOnClickListener(newView.OnClickListener() { @Offset public blank onClick(view) { // Snackbar Snackbar Snackbar = Create and show a snackbar.make(getView(), message is deleted, Snackbar.LENGTH_SHORT); // Add an action Click Retest Listener snackbar.setAction(Undo, new UndoListener()); // Show Snackbar snackbar.show(); }. ))); customViewSnackbar.setOnClickListener(new View.OnClickListener() { @Overwrite public blank onClick (Show View) { // Snackbar Snackbar Create and show snackbar = Snackbar.make(getView()), text style, action font and color, snackbar.LENGTH_SHORT); // Set the snackbar text color. setActionTextColor( ContextCompat.getColor(getBaseContext(), R.color.actionTextColor); snackbar.setAction(Undo, new UndoListener()); Show SnackbarView = Snackbar.getView(); Text View textView = snackbarView.findViewById(R.id.snackbar_text); // set the number of text lines textView.setMaxLines(2); // setTextColor(ContextCompat.getColor(getBaseContext(), R.color.actionTextColor); // set the text size in textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); // set the background color snackbarView.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorBg)); snackbar.show(); }. }); } // The UndoListener closed class definition for clicks implements View.OnClickListener { @overwrite public blank onClick(view v) { // Code to undo the last user action View contextView = findViewById(android).R.id.content); // Snackbar Create and display Snackbar.make (contextView, message restore, Snackbar.LENGTH_SHORT) .show(); }; } }. } /** * Returns the view instance * * @Return CoordinatorLayout */ public view getView() { findViewById(android.R.id.content) return; } } }.

Package com.example.snackbar ;

Import android.os.bundle; import
android.util.TypedValue; import
android.view.view; import
android.widget.Button; import
android.widget.TextView;
-Import androidx.appcompat.appCompatActivity;
-Import androidx.core.content.ContextCompatible;
-Import com.google.android.material.snackbar ;

the MainActivity public class expands AppCompatActivity {

Write
protected blank onCreate(storedInstanceState bundle) {
super.onCreate(storedInstanceState);
setContentView(R.layout.activity_main) ;

SimpleSnackbar button = findViewById(R.id.buttonSimple) ;
ActionButtonSnackbar button = findViewById(R.id.buttonAction) ;
CustomViewSnackbar button = findViewById(R.id.buttonCustomView) ;

// Simple Snackbar
Code fragment simpleSnackbar.setOnClickListener(new view.OnClickListener() {
@Cancel public space onClick(view) { view) { view).

// Create and display the
Snackbar.make(getView(), Android snackbar.LENGTH_SHORT)
.show();
}
}) ;

actionButtonSnackbar.setOnClickListener(new view.OnClickListener() {@Define public blank onClick(view) {// SnackbarSnackbar = Snackbar.make(getView(), Delete message, Snackbar.LENGTH_SHORT);// Click Repeat Listener Actionsnackbar.setAction(Undo, new UndoListener());// show Snackbarsnackbar.show();}.}) ;

customViewSnackbar.setOnClickListener(new view.OnClickListener() {
@Overwrite public blank onclick(show view)) {

// Create and specify snackbar
Snackbar Snackbar = Snackbar.make(getView(), text style, font and action color,
Snackbar.LENGTH_SHORT) ;

// Set the color of the action text
snackbar.setActionTextColor(
ContextCompat.getColor(getBaseContext(), R.color.actionTextColor)
) ;
snackbar.setAction(UndoListener(), new UndoListener())

View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(R.id.snackbar_text);
// Set the number of text lines in
textView.setMaxLines(2);
// and text color
textView.setTextColor(ContextCompatible.getColor(getBaseContext(), R.color.actionTextColor))));
//and text size
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//Set sniffer background color
snackbarView.setBackgroundColor(getBaseContext(), R.color.colorBg)));
snackbar.show();
}.
});
}).

// Click on Listener Definition
The private class UndoListener implements the display. OnClickListener {

@Suchepublic void onClick(view v) {// Code to undo last user actionSee contextView = findViewById(android.R.id.content);// Make(contextView, message restore, Snackbar.LENGTH_SHORT).show();}Snackbar.}

/**
* return view
*
* @ return coordinator layout
*/
public view getView() {
return findViewById(android.R.id.content);
}
}

Conclusion

In this blog we discovered the different ways to set up an Android Snackbar. I hope you enjoyed the article. Help me by sharing this message with all your friends who are developing Android applications.

Search solution code

Read our tutorial on Android Seriesarchitecture components

 

 

 android snackbar example without coordinatorlayout,snackbar android kotlin,custom snackbar android,snackbar android dependency,top snackbar android,popup message in android example,android snackbar vs toast,dismiss snackbar android

You May Also Like