How to add a Snackbar in Android

Snackbar offers brief commentary on an activity. On smaller devices, the notice shows at the bottom of the screen; on larger ones, it appears lower left. Above every element on the screen is the Snackbar. However, it has no effect on any component. Snackbar can only function properly when a CoordinatorLayout is present in your view hierarchy. This is how features like swipe-to-dismiss and widget movement automation work.

In this article, you will learn how to create your Snackbar in Android. So let’s take example of Snackbar codes  in Android of Mobile application.

Example of Snackbar

                                          

 Step-by-Step Implementation

 Step 1: Create a New Project in Android ​studio (File >new  project). 


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file. 

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

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Open Snackbar"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​lowing code inside MainActivity.kt file.

 import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.snackbar.Snackbar


class MainActivity : AppCompatActivity() {
var layout: ConstraintLayout? = null
lateinit var button: Button

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button = findViewById(R.id.button)
layout = findViewById(R.id.layout);
val text = "Candidroot Solutions"
button.setOnClickListener {
showToast(text)
}
}

@SuppressLint("ResourceType")
private fun showToast(text: String) {
val snackbar = layout?.let {
Snackbar
.make(
it,
text,
Snackbar.LENGTH_LONG
)
.setAction(
"UNDO"

)

{
Toast
.makeText(
this@MainActivity,
"Undo Clicked",
Toast.LENGTH_SHORT
)
.show()
}
}

snackbar?.show()

}
}

Step 4 : Output of above example.

                                                        

Happy coding!

365Bloggy July 17, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive