Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show SnackBar in BottomSheetDialogFragment?

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>
like image 820
Hamed Ghadirian Avatar asked Jun 21 '17 05:06

Hamed Ghadirian


People also ask

How do I get view for snackbar in activity?

The code snippet for Action Call Snackbar button is given below: two. setOnClickListener(new View. OnClickListener() { @Override public void onClick(View v) { Snackbar snackbar = Snackbar .


2 Answers

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.
like image 55
ror Avatar answered Sep 19 '22 15:09

ror


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

Add this peace of code to your onCreateView

like image 24
Wimukthi Rajapaksha Avatar answered Sep 20 '22 15:09

Wimukthi Rajapaksha