15

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.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16
  • technically speaking, it should be a problem. The real question is why would one need that – Blackbelt Sep 08 '15 at 10:05
  • I don't think you can use Snackbar in dialog because it always display on the bottom of the screen. In addition, you shouldn't use Snackbar in dialog I must say because it's not the right way to use of it. – Nooruddin Lakhani Sep 08 '15 at 10:32

4 Answers4

21

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.

reVerse
  • 35,075
  • 22
  • 89
  • 84
5

Yes, you can.

To show Snackbar inside your Dialog create custom View for it. You can read more about it here: Dialogs/Creating a Custom Layout

Then for showing Snackbar invoke Snackbar.make((dialogView, "text", duration)) where dialogView is your custom view.

Ilya Tretyakov
  • 6,848
  • 3
  • 28
  • 45
1

If you're using a Dialog then:

dialog_share = new Dialog(MainScreen.this, R.style.DialogTheme);
dialog_share.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = this.getLayoutInflater();
mDialogView = inflater.inflate(R.layout.dialog_share, null);
dialog_share.setContentView(mDialogView);
dialog_share.getWindow().setBackgroundDrawableResource(R.color.translucent_black);
dialog_share.show();

public void ShowSnackBarNoInternetOverDialog() {
        Snackbar snackbar = Snackbar.make(mDialogView, getString(R.string.checkinternet), Snackbar.LENGTH_LONG);
        snackbar.setActionTextColor(Color.CYAN);
        snackbar.setAction("OK", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainScreen.this, "snackbar OK clicked", Toast.LENGTH_LONG).show();
            }
        });
        snackbar.show();
    }
erdomester
  • 11,789
  • 32
  • 132
  • 234
1

Use getDialog().getWindow().getDecorView() inside Snackbar.make()

Snackbar
      .make(getDialog().getWindow().getDecorView(), "SnackBar in Dialog", Snackbar.LENGTH_LONG)
      .show();
P1NG2WIN
  • 764
  • 1
  • 9
  • 24