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
<?xmlversion="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
packageorg.neveropen.gfgsnackbar;ÂÂimportandroidx.appcompat.app.AppCompatActivity;importandroidx.coordinatorlayout   Â.widget.CoordinatorLayout;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.Toast;ÂÂimportcom.google.android.material   Â.snackbar   Â.Snackbar;ÂÂpublicclassMainActivity   ÂextendsAppCompatActivity {   ÂButton button;   ÂCoordinatorLayout layout;   Â@Override   ÂprotectedvoidonCreate(       ÂBundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       Âbutton = findViewById(R.id.button);       Âlayout = findViewById(R.id.layout);       Âbutton.setOnClickListener(           ÂnewView.OnClickListener() {               Â@Override               ÂpublicvoidonClick(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                                 ÂnewView.OnClickListener() {                                     Â@Override                                     ÂpublicvoidonClick(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 […]