25

I search alot but couldn't find any solutionCan I display material design Snackbar in dialog? and Snackbar is not working within fragment class doesn't help. I pass rootView of fragment and also try passing a view from getActivity but none of them works!

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);

Snackbar.make(MyActivity.myTextview, "Hello", Snackbar.LENGTH_INDEFINITE).show();

Snackbar.make(rootView, "Hello", Snackbar.LENGTH_INDEFINITE).show();

return rootView;

}

and my content_dialog_bottom_sheet :

 <RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/bottomSheetLayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/background"
   app:behavior_hideable="true"
   app:behavior_peekHeight="180dp"
   app:layout_behavior="@string/bottom_sheet_behavior">

  //some views 

</RelativeLayout>
Hamed Ghadirian
  • 6,159
  • 7
  • 48
  • 67

5 Answers5

50

Solution is fairly simple. You need to:

  1. Wrap your dialog layout with CoordinatorLayout (and if you want to show snackbar just next to some specific view, wrap it instead)
  2. Use CoordinatorLayout id as view while showing snackbar.
ror
  • 3,295
  • 1
  • 19
  • 27
21
Snackbar.make(
            getDialog().getWindow().getDecorView(),
            "your-string",
            Snackbar.LENGTH_SHORT
    ).show();

Add this peace of code to your onCreateView

Wimukthi Rajapaksha
  • 961
  • 1
  • 11
  • 23
2

Show snackbar after a delay:

   new Handler().postDelayed(new Runnable() {
  @Override
        public void run() {
            Snackbar.make(rootView, "Hello", Snackbar.LENGTH_INDEFINITE).show();

        }
    },200);
Sonam Gupta
  • 341
  • 1
  • 12
2

If you are extending your Bottomsheet class to BottomSheetDialogFragment(), you can use dialog's decorView as a view.

Sample Code Snippet for reference :

Snackbar.make(
                            dialog?.window?.decorView,
                            "Press login button to continue",
                            Snackbar.LENGTH_LONG
                        )
                            .setAction("Login") { v: View? ->
                                val intent = Intent(context, DestinationActivityAfterLogin::class.java)
                                startActivity(intent)
                            }.show()

====

If you are using BottomSheetDialog for showing the bottomsheet, there one can use rootView to pop-up snackbar.

Sample code for reference :

Snackbar.make(
                bsBinding.root.rootView, // bsBinding is view binding object
                "Press login button to continue",
                            Snackbar.LENGTH_LONG
                        )
                            .setAction("Login") { v: View? ->
                                val intent = Intent(context, DestinationActivityAfterLogin::class.java)
                                startActivity(intent)
                            }.show()

Hope this would be useful while showing snackbar with bottom sheet dialogs.

Happy coding.

Abhijeet
  • 501
  • 4
  • 7
1

using @Wimukthi Rajapaksha answer:

Snackbar.make(
        getDialog().getWindow().getDecorView(),
        "your-string",
        Snackbar.LENGTH_SHORT
).show();

you can use the following code line to show it above a certain view, make sure to show the snack bar after setting anchor view not before.

snackBar.anchorView = -Your view-

Abed Sako
  • 23
  • 4