Show Snackbar - Android Studio - Kotlin
How to show a Snackbar and a Snackbar with an Action Button?
Learn how to easily display a Snackbar in your Android app using Kotlin 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.kt
package com.technifysoft.myapplication import android.os.Bundle import android.view.View import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.material.button.MaterialButton import com.google.android.material.snackbar.Snackbar class MainActivityy : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Find the buttons from the layout XML val snackbarBtn = findViewById<MaterialButton?>(R.id.snackbarBtn) val snackbarActionBtn = findViewById<MaterialButton?>(R.id.snackbarActionBtn) // Set an OnClickListener for the snackbar button snackbarBtn?.setOnClickListener { // Call the showSnackbar method when the button is clicked showSnackbar(it) } // Set an OnClickListener for the snackbar with action button snackbarActionBtn?.setOnClickListener { // Call the showSnackbarWithAction method when the button is clicked showSnackbarWithAction(it) } } // Method to show a simple Snackbar private fun 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 fun 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") { // Handle action click by showing a Toast message Toast.makeText(this@MainActivityy, "Action Clicked!", Toast.LENGTH_SHORT).show() } .show() } }
Comments
Post a Comment