Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CustomSnackbar [Android Kotlin]
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar
//import your.package.databinding.SnackbarBinding
class CustomSnackbar(private val view: View) {
fun showSnackBar(title: String, cancelFun: () -> Unit = {}) {
val snackView = View.inflate(view.context, R.layout.snackbar, null)
val binding = SnackbarBinding.bind(snackView)
val snackbar = Snackbar.make(view, "", Snackbar.LENGTH_LONG)
(snackbar.view as ViewGroup).removeAllViews()
(snackbar.view as ViewGroup).addView(binding.root)
snackbar.view.setPadding(0, 0, 0, 0)
snackbar.view.elevation = 0f
snackbar.setBackgroundTint(
ContextCompat.getColor(
view.context,
android.R.color.transparent
)
)
binding.tvTitle.text = title
binding.btnCancel.setOnClickListener {
cancelFun()
snackbar.dismiss()
}
snackbar.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment