info@androidpaper.co.in

How Android Snack Bar Displaying Short Messages in Your App

Snackbar is a component in Android that provides a lightweight and non-intrusive way to display short messages or notifications to the user. It is typically used to show messages that are important but not critical, such as a confirmation message or a simple status update. Snackbars are displayed at the bottom of the screen and automatically disappear after a certain duration or when the user dismisses them.

To display a Snackbar in your Android app, you can follow these steps: Get a reference to the CoordinatorLayout or View where you want to display the Snackbar. The CoordinatorLayout is commonly used because it provides support for Snackbar's behavior. Create an instance of the Snackbar class using the make() method, passing in the parent view, message, and duration.

Snackbar snackbar = Snackbar.make(coordinatorLayout, "Your message here", Snackbar.LENGTH_SHORT);

Create a new activity or open an existing one in Android Studio. Open the XML layout file for the activity and add a CoordinatorLayout as the root element. This layout will serve as the parent view for displaying the Snackbar.

<?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"
    android:id="@+id/layout"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="100dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:text="Click the below Button to how Snackbar" />

        <Button
            android:id="@+id/callbackButton"
            android:layout_width="300dp"
            android:layout_height="80dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="50dp"
            android:backgroundTint="@android:color/holo_blue_light"
            android:textColor="@android:color/white"
            android:text="Tap To Show Snackbar Message"/>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

In your activity code, create a method to display the Snackbar when needed. For example, you can call this method in response to a button click.

Button button ;
 ConstraintLayout layout;

button = (Button) findViewById(R.id.callbackButton);
 layout = findViewById(R.id.layout);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                
            }
        });

Add a button or any other UI element to your XML layout, and set an onClick attribute to call the showSnackbar() method.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Snackbar"
    android:onClick="showSnackbar" />

In the code, we have implemented a button tap event using an OnClickListener, and when the button is tapped, the showSnackbar() function is called to display the Snackbar message on the screen.

                button.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v)
                            {

                                // Create a snackbar
                                Snackbar snackbar
                                        = Snackbar
                                        .make(
                                                layout,
                                                "Message is deleted",
                                                Snackbar.LENGTH_LONG)
                                                .setAction(
                                                "UNDO",

                                                new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view)
                                                    {
                                                        Toast
                                                                .makeText(
                                                                        SnackbarActivity.this,
                                                                        "Undo Clicked",
                                                                        Toast.LENGTH_SHORT)
                                                                .show();
                                                    }
                                                });

                                snackbar.show();
                            }
                        });

Another Written Description

Make sure to adjust the layout and other elements according to your app's specific requirements. By following these steps, you can display a Snackbar in your Android app. When the button is clicked, the showSnackbar() method is called, and the Snackbar with the specified message and optional action is displayed at the bottom of the screen. Remember to handle any necessary actions in the onClick() method of the Snackbar action button.