Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I display material design Snackbar in dialog?

I am developing an android application. In that I want to display material design Snackbar in dialog. Is it possible? If yes then how?

Please help me.

Thanks.

like image 674
Ganpat Kaliya Avatar asked Sep 08 '15 09:09

Ganpat Kaliya


1 Answers

It's definitely possible, you just have to pass the View of the Dialog to the SnackBar.

Example

    AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    // inflate the custom dialog view
    final View mDialogView = inflater.inflate(R.layout.dialog_layout, null);
    // set the View for the AlertDialog
    mAlertDialogBuilder.setView(mDialogView);

    Button btn = (Button) mDialogView.findViewById(R.id.dialog_btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Pass the mDialogView to the SnackBar
            Snackbar
                    .make(mDialogView, "SnackBar in Dialog", Snackbar.LENGTH_LONG)
                    .show();
        }
    });
    AlertDialog alertDialog = mAlertDialogBuilder.create();
    alertDialog.show();

Result

picture showing result

Note: There's no need to use a CoordinatorLayout as the root. In my example I simply used a LinearLayout as the root.

like image 74
reVerse Avatar answered Oct 07 '22 14:10

reVerse