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

#2196F3>Color>

#1976D2>/ Color>

#FFEB3B

####E64A19

#FBE9E7

> 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



#2196F3
#1976D2
#FFEB3B>Color>.
#E64A19>Color>
#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.

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

[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