A utils function for show snack bar
fun showSnackBar(activity: Activity, message: String, action: String? = null,
actionListener: View.OnClickListener? = null, duration: Int = Snackbar.LENGTH_SHORT) {
val snackBar = Snackbar.make(activity.findViewById(android.R.id.content), message, duration)
.setBackgroundColor(Color.parseColor("#CC000000")) // todo update your color
.setTextColor(Color.WHITE)
if (action != null && actionListener!=null) {
snackBar.setAction(action, actionListener)
}
snackBar.show()
}
Example using in Activity
showSnackBar(this, "No internet")
showSnackBar(this, "No internet", duration = Snackbar.LENGTH_LONG)
showSnackBar(activity, "No internet", "OK", View.OnClickListener {
// handle click
})
Example using in Fragment
showSnackBar(getActivity(), "No internet")
Hope it help