Snackbar provides lightweight feedback about an operation. The message appears at the bottom of the screen on mobile and lower left on larger devices. Snackbar appears above all the elements of the screen. But no component is affected by it. Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets. Snackbar is similar to Toast but the only major difference is that an action can be added with Snackbar.
Approach:
- Add the support Library in build.gradle file and add Material Design dependency in the dependencies section.It is a part of Material Design that’s why we have to add a dependency.
dependencies {
   Â
implementation 'com.google.android.material:material:1.1.0'
}
- Now add the following code in the activity_main.xml file. It will create a button named Open Snackbar.
activity_main.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
androidx.coordinatorlayout.widget.CoordinatorLayout
   Â
android:layout_width
=
"match_parent"
   Â
android:layout_height
=
"match_parent"
   Â
android:id
=
"@+id/layout"
   Â
tools:context
=
".MainActivity"
>
Â
ÂÂ Â Â Â
<
Button
       Â
android:layout_gravity
=
"center"
       Â
android:id
=
"@+id/button"
       Â
android:layout_width
=
"wrap_content"
       Â
android:layout_height
=
"wrap_content"
       Â
android:textSize
=
"18sp"
       Â
android:textAllCaps
=
"false"
       Â
android:text
=
"Open Snackbar"
        Â
/>
Â
Â</
androidx.coordinatorlayout.widget.CoordinatorLayout
>
- Now add the following code in the MainActivity.java file. This will define the button and add a onClickListener to the button. In the onClickListener a Snackbar is created and is called. So whenever the button is clicked, the onClickListener creates a snackbar and calls it and the user sees the message. This snackbar contains an action and if clicked will show a toast.
MainActivity.java
package
org.neveropen.gfgsnackbar;
Â
Âimport
androidx.appcompat.app.AppCompatActivity;
import
androidx.coordinatorlayout
   Â
.widget.CoordinatorLayout;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
Â
Âimport
com.google.android.material
   Â
.snackbar
   Â
.Snackbar;
Â
Âpublic
class
MainActivity
   Â
extends
AppCompatActivity {
Â
ÂÂ Â Â Â
Button button;
   Â
CoordinatorLayout layout;
Â
ÂÂ Â Â Â
@Override
   Â
protected
void
onCreate(
       Â
Bundle savedInstanceState)
   Â
{
       Â
super
.onCreate(savedInstanceState);
       Â
setContentView(R.layout.activity_main);
Â
ÂÂ Â Â Â Â Â Â Â
button = findViewById(R.id.button);
       Â
layout = findViewById(R.id.layout);
Â
ÂÂ Â Â Â Â Â Â Â
button.setOnClickListener(
           Â
new
View.OnClickListener() {
               Â
@Override
               Â
public
void
onClick(View v)
               Â
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
// Create a snackbar
                   Â
Snackbar snackbar
                       Â
= Snackbar
                             Â
.make(
                                 Â
layout,
                                 Â
"Message is deleted"
,
                                 Â
Snackbar.LENGTH_LONG)
                             Â
.setAction(
                                 Â
"UNDO"
,
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
// If the Undo button
                                 Â
// is pressed, show
                                 Â
// the message using Toast
                                 Â
new
View.OnClickListener() {
                                     Â
@Override
                                     Â
public
void
onClick(View view)
                                     Â
{
                                         Â
Toast
                                             Â
.makeText(
                                                 Â
MainActivity.
this
,
                                                 Â
"Undo Clicked"
,
                                                 Â
Toast.LENGTH_SHORT)
                                             Â
.show();
                                     Â
}
                                 Â
});
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
snackbar.show();
               Â
}
           Â
});
   Â
}
}
Output:
[…] a popup message with the help of Alerter Library. It is better to use Alerter than using Toast or Snackbar in cases if some alert messages are to be displayed to the user. We can add various onClickListners […]