Make custom view like snackbar on top andorid năm 2024

In this tutorial we’ll discuss and implement various forms of Android Snackbar widget in our application.

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.

  1. A Toast messages can be customised and printed anywhere on the screen, but a Snackbar can be only showed in the bottom of the screen
  2. A Toast message don’t have action button, but Snackbar may have action button optionally. Though, A Snackbar shouldn’t have more than one action button
  3. Toast message cannot be off until the time limit finish, but Snackbar can be swiped off before the time limit

Note: Toast message and Snackbar have display length property in common. A code snippet to display a basic android Snackbar is shown below.

Snackbar snackbar = Snackbar
        .make(coordinatorLayout, "www.journaldev.com", Snackbar.LENGTH_LONG);
snackbar.show();

In the above snippet make() method accepts three parameters:

  1. coordinatorLayout : It is the root layout of the activity
  2. www.journaldev.com : This is the message to be appear on snackbar, and we can customise it with our own message
  3. Snackbar.LENGH_LONG : This is last parameter which is the time limit how long snackbar to be displayed

show() method is used to display the Snackbar on the screen.

Make custom view like snackbar on top andorid năm 2024

No changes in the activity_main.xml code which contains the CoordinatorLayout. The content_main.xml consists of three buttons. One for each type of Snackbar that we’ll be discussing.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.journaldev.snackbar.MainActivity"
    tools:showIn="@layout/activity_main">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DEFAULT SNACKBAR"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ACTION CALL SNACKBAR"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CUSTOM VIEW SNACKBAR"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

The code snippet for Action Call Snackbar button is given below:

two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar
                        .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
                        .setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);
                                snackbar1.show();
                            }
                        });
                snackbar.show();
            }
        });

In the above code a new onClickListener method is invoked on clicking the action button with the respective Snackbar being displayed in it. The code snippet for Custom Snackbar that’s invoked on the second button is given below:

A Snackbar is a widget that looks like a small banner that pops up at the bottom of the user’s phone screen. … Android Snackbar is just like a Toast in Android except that it can provide the user with the action button to interact with. You may call the Snackbar a toast widget with an optional action button.

How do I use a snack bar?

This will define the button and add an on-click listener to the button. In the on-click listener, a Snackbar is created and is called. So whenever the button is clicked, the on-click listener creates a snack bar and calls it and the user sees the message. This snack bar contains action and if clicked will show a toast.

Difference between Toast and Snackbar

  1. A Toast message can be customized and printed anywhere on the screen, but a Snackbar can be only shown at the bottom of the screen
  2. A Toast message doesn’t have an action button, but Snackbar may have an action button optionally. Though, A Snackbar shouldn’t have more than one action button
  3. Toast message cannot be off until the time limit finish, but Snackbar can be swiped off before the time limit.

Using Normal Snackbar

To use a Snackbar in your app, all you need to do is have the Material Design dependency in your app. So, add the below dependency in you build.gradle file:

implementation "com.google.android.material:$latest_version"

And then you can use the Snackbar just like a Toast. For example:

Snackbar.make(view, "Show some message here", Snackbar.LENGTH_SHORT).show()

The above code will show a simple message in the Snackbar. Some functions are available with Snackbar. For example:

How do I set custom view to snackbar on Android?

Steps to Implement the Custom SnackBars in Android.

Step 1: Create an empty activity project..

Step 2: Working with the activity_main.xml file..

Step 3: Creating a custom layout for Snackbar..

Step 4: Working with the MainActivity.java file..

How do I make a snackbar on my Android?

Snackbar Example In Android Studio:.

Step 1: Create a new project and name is SnackbarExample..

Step 2: Open build. gradle (Module: app) and add the below design support library for your project. ... .

Step 3: Now open activity_main. xml and enter the below code:.

Step 4: Open MainActivity. java and add the below code:.

Output:.

How do I change my snackbar position?

Change the Position of Snackbar In the example above, the Snackbar is positioned at the top center of the screen by setting the contentAlignment parameter to Alignment. Center. You can change this to any desired position according to your app's requirements such as Alignment. TopCenter, Alignment.