snackbar kotlin

// Step 1: Import the necessary Snackbar library
import com.google.android.material.snackbar.Snackbar
import android.view.View

// Step 2: Create a function to display the Snackbar
fun showSnackbar(view: View, message: String) {
    // Step 3: Create a Snackbar instance with the provided message and duration
    val snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT)

    // Step 4: Optionally, add an action to the Snackbar
    snackbar.setAction("Undo") {
        // Action code here (e.g., undo operation)
    }

    // Step 5: Optionally, customize the Snackbar's appearance and behavior
    snackbar.setTextColor(resources.getColor(R.color.snackbar_text_color))
    snackbar.setBackgroundTint(resources.getColor(R.color.snackbar_background_color))
    snackbar.animationMode = Snackbar.ANIMATION_MODE_SLIDE

    // Step 6: Show the Snackbar
    snackbar.show()
}

// Step 7: Call the showSnackbar function where needed, passing the desired view and message
// For example, in an activity or fragment:
// showSnackbar(findViewById(android.R.id.content), "Snackbar Message")