Show Snackbar - Android Studio - Java
How to show a Snackbar and a Snackbar with an Action Button?
Learn how to easily display a Snackbar in your Android app using Java in Android Studio. A Snackbar is a lightweight feedback mechanism that appears at the bottom of the screen to show quick messages to users. In this step-by-step tutorial, you’ll understand how to create, customize, and add actions to a Snackbar, making your app more interactive and user-friendly. Perfect for beginners and developers who want to enhance their Android UI/UX with modern material design components.
>> Check for Java
>> Check for Kotlin
>> Check for Compose
Code
Here is the full code to show a Snackbar and a Snackbar with an Action Button
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rootLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <com.google.android.material.button.MaterialButton android:id="@+id/snackbarBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginHorizontal="30dp" android:text="Show Snackbar" /> <com.google.android.material.button.MaterialButton android:id="@+id/snackbarActionBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginHorizontal="30dp" android:text="Show Snackbar with Action button" /> </LinearLayout>
MainActivity.java
package com.technifysoft.myapplication; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.button.MaterialButton; import com.google.android.material.snackbar.Snackbar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find the buttons from the layout XML MaterialButton snackbarBtn = findViewById(R.id.snackbarBtn); MaterialButton snackbarActionBtn = findViewById(R.id.snackbarActionBtn); // Set an OnClickListener for the snackbar button snackbarBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Call the showSnackbar method when the button is clicked showSnackbar(view); } }); // Set an OnClickListener for the snackbar with action button snackbarActionBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Call the showSnackbarWithAction method when the button is clicked showSnackbarWithAction(view); } }); } // Method to show a simple Snackbar private void showSnackbar(View view) { // Create and show a Snackbar with a short duration Snackbar.make(view, "This is a simple Snackbar message", Snackbar.LENGTH_SHORT).show(); } // Method to show a Snackbar with an action private void showSnackbarWithAction(View view) { // Create a Snackbar with a long duration and an action Snackbar.make(view, "Snackbar Message with Action", Snackbar.LENGTH_LONG) .setAction("My Action", new View.OnClickListener() { @Override public void onClick(View view) { // Handle action click by showing a Toast message Toast.makeText(MainActivity.this, "Action Clicked!", Toast.LENGTH_SHORT).show(); } }) .show(); } }
Screenshot

Comments
Post a Comment