Can I display a Snackbar design construct in a dialog box?

I am developing an Android application. That I want to display the Snackbar design structure in a dialog box. Is it possible? If so, how?

Please help me.

Thanks.

+6
source share
3 answers

This is definitely possible, you just need to pass the view of the SnackBar dialog box.

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

image showing the result

Note : There is no need to use CoordinatorLayout root. In my example, I just used LinearLayout as the root.

+9
source

Yes, you can.

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

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

+4
source

If you are using 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(); } 
0
source

All Articles