简体   繁体   中英

Android: How I get dialog object is showing in an activity

I have a question about the dialog in android. I have an activity object and I want to know what dialog is showing on activity. My problem is I want to show a snackbar when application has a warning message. And I need show snackbar in activity. But from activity, when I get content view and pass to SnackBar (SnackBar.make(contentView, "text", duration). It show Snackbar below the dialog. I know we should get view from dialog to show Snackbar from dialog view.

Please help me for this case.

Regards, Duc Trinh

Check out the params passed to make() method -

make (View view, CharSequence text, int duration)

View: The view to find a parent from. Snackbar will find parent of view passed to show.

You need to pass child element of dialogView in order to show Snackbar in Dialog. Consider passing -

Snackbar.make(mDialogView.findViewById(R.id.child_view), "SnackBar in Dialog", Snackbar.LENGTH_LONG)
                    .show();

After dialog is declared globally and has already been initialized , try looking this:

if(dialog != null || dialog.isShowing()){   
  // Dialog is being shown or not null 
}

you have to pass the View of the Dialog to the SnackBar.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM