Snackbar in Android

Snackbar in Android with Example

Snackbar in Android:

Snackbar is a widget that looks like a small banner that appears at the bottom of the user’s phone screen. Android Snack Bar is similar to Android Toast, except that it can provide operation buttons that users can interact with. Snackbar is a toast widget with additional action buttons.

Note: The default background color of snackbar in android is “#323232” and the default text color is “#FFFFFF“.

Difference between Toast and Snackbar

Toast Snackbar
Toasts (Android only) are mainly used for system messages.The snack bar contains a single line of text directly related to the operation being performed.
hey are also displayed at the bottom of the screen, but cannot be removed from the screen.They can contain text operations, but not symbols.

Learn Java Programming & get a Certificate

Click to download

Snackbar Code:

val snack = Snackbar.make(it,"Developers Dome Snackbar Example",Snackbar.LENGTH_LONG)
snack.show()

Preview: Snackbar

dependencies :

–>Add the code given below to your file build.gradle(app).

dependencies {
    ...
    implementation 'com.android.support:design:26.1.0'
    ...
}

File: MainActivity.kt

–> In MainActivity.kt file initialize variable and implement snackbar methods.

package com.example.snackbar

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.snackbar.Snackbar.make

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title="Snackbar"
         val btn =findViewById<Button>(R.id.btnSnackBar)
        btn.setOnClickListener {
            val snackbar=Snackbar.make(it,"Replace with your action ",Snackbar.LENGTH_LONG).setAction("Action",null)
            snackbar.setActionTextColor(Color.BLACK)
            snackbar.setGestureInsetBottomIgnored(true)
            val snackBarView=snackbar.view

            snackBarView.setBackgroundColor(Color.BLACK)
            val textView=snackBarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
            textView.setTextColor(Color.WHITE)
            textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_baseline_undo_24, 0, 0, 0);
            textView.setCompoundDrawablePadding(getResources().getDimensionPixelOffset(R.dimen.snackbar_icon_padding))
            snackbar.show()
        }

    }
}

File: activity_main.xml

–>activity_main.xml contains the only button for the snackbar.

<?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:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnSnackBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="209dp"
        android:layout_marginEnd="71dp"
        android:layout_marginRight="71dp"
        android:text="Tap To Display SnackBar"
        android:textColor="@android:color/background_dark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • We hope that this guide will assist you in quickly creating a snackbar in the android application and still If you have any problems or queries, please post them in the comments section and we will gladly assist you.

Leave a Reply