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.
Solution 1:
It's definitely possible, you just have to pass the View of the Dialog to the SnackBar
.
Example
AlertDialog.BuildermAlertDialogBuilder=newAlertDialog.Builder(this);
LayoutInflaterinflater=this.getLayoutInflater();
// inflate the custom dialog viewfinalViewmDialogView= inflater.inflate(R.layout.dialog_layout, null);
// set the View for the AlertDialog
mAlertDialogBuilder.setView(mDialogView);
Buttonbtn= (Button) mDialogView.findViewById(R.id.dialog_btn);
btn.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
// Pass the mDialogView to the SnackBar
Snackbar
.make(mDialogView, "SnackBar in Dialog", Snackbar.LENGTH_LONG)
.show();
}
});
AlertDialogalertDialog= mAlertDialogBuilder.create();
alertDialog.show();
Result
Note:
There's no need to use a CoordinatorLayout
as the root. In my example I simply used a LinearLayout as the root.
Solution 2:
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.
Solution 3:
If you're using a Dialog
then:
dialog_share = newDialog(MainScreen.this, R.style.DialogTheme);
dialog_share.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflaterinflater=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();
publicvoidShowSnackBarNoInternetOverDialog() {
Snackbarsnackbar= Snackbar.make(mDialogView, getString(R.string.checkinternet), Snackbar.LENGTH_LONG);
snackbar.setActionTextColor(Color.CYAN);
snackbar.setAction("OK", newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
//Toast.makeText(MainScreen.this, "snackbar OK clicked", Toast.LENGTH_LONG).show();
}
});
snackbar.show();
}
Solution 4:
Use getDialog().getWindow().getDecorView()
inside Snackbar.make()
Snackbar
.make(getDialog().getWindow().getDecorView(), "SnackBar in Dialog", Snackbar.LENGTH_LONG)
.show();
Post a Comment for "Can I Display Material Design Snackbar In Dialog?"