Toast and Snackbar with Material Design 3

Toast and Snackbar with Material Design 3

Toast

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.

Toast is limited to two lines of text and shows the application icon next to the text. Be aware that the line length of this text varies by screen size, so it's good to make the text as short as possible.

If your app is in the foreground, consider using a snackbar instead of using a toast. Snackbars include user-actionable options, which can provide a better app experience.

If your app is in the background, and you want users to take some action, use a notification instead.

 How to create app using Jetpack compose

 Snackbar

Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

Snackbars automatically disappear after a timeout. They can also be dismissed by being swiped off screen, by action click, from a new snackbar being displayed, or manually via a call to dismiss().

Steps

For this project we are using Android Studio Latest version Flamingo.
Create a New Project and select Empty Views Activity 
Write the application name as Snackbar Toast.


layout/activity_main.xml



MainActivity.kt

package com.example.snackbartoast

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.android.material.button.MaterialButton
import com.google.android.material.snackbar.Snackbar

class MainActivity : AppCompatActivity() {
   
override fun onCreate(savedInstanceState: Bundle?) {
       
super.onCreate(savedInstanceState)
        setContentView(
R.layout.activity_main)
        
val showToast=findViewById<MaterialButton>(R.id.showToast)
       
val showSnackbar=findViewById<MaterialButton>(R.id.showSnackbar)

       
showToast.setOnClickListener {
           
Toast.makeText(this, "Hey, I am Android Toast", Toast.LENGTH_SHORT).show()
        }

        showSnackbar.setOnClickListener {
           
val snack = Snackbar.make(it,"Iam a simple Snackbar",Snackbar.LENGTH_LONG)
           
snack.show()}}}

If you want to change the text color of your snackbar here is the custom code

showSnackbar.setOnClickListener {

val snackBar = Snackbar.make(

     it, "Hey, Iam Android Snackbar",
    
Snackbar.LENGTH_LONG
 
).setAction("Action", null)
 
snackBar.setActionTextColor(Color.MAGENTA)
 
val snackBarView = snackBar.view
 
val textView = snackBarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView

 textView.setTextColor(Color.MAGENTA)
 
snackBar.show()

}

 




Thanks for reading!😊😊😊😊😊😊
If you enjoyed reading , Show some Love by
 clapping 👏 on this post!
If you have any suggestion share with me.

Post a Comment

0 Comments