android snackbar without view code example

Example 1: latest dependency for snackbar android

implementation "com.google.android.material:material:1.1.0-alpha06"

Example 2: android create snackbar

Snackbar.make(fragmentView, text, Snackbar.LENGTH_LONG).setAction("Action", null /* replace with your action or leave null to just display text*/).show();

Example 3: android snackbar dismiss

final Snackbar snackBar = Snackbar.make(findViewById(android.R.id.content), "Snackbar Message", Snackbar.LENGTH_LONG);

        snackBar.setAction("Action Message", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Call your action method here
                snackBar.dismiss();
            }
        });
        snackBar.show();

Tags:

Java Example