2

I am validating an AlertDialog, and I would like to raise a Toast on top of the AlertDialog display.

I have this code, but the Toast is displayed on the activity

new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
                .setView(addView).setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {

                                if (wrapper.getTitle().length()>0)
                                {
                                    processAdd(wrapper);
                                } else {
                                    Toast.makeText(getApplicationContext(), "Name is required", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }).setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                // ignore, just dismiss
                            }
                        }).show();
Pentium10
  • 204,586
  • 122
  • 423
  • 502

3 Answers3

8

Instead of using AdvertDialog.Builder, you can create a custom dialog which will behave like a dialog, but is in fact a normal activity. Your toasts should be drawn normally on top of this.

seanhodges
  • 17,426
  • 15
  • 71
  • 93
  • That is the best way to do it I'd imagane, the AlertDialog builder builds the Dialog as part of the activity so the toast appears on the activity then, at least thats what i believe happens – Donal Rafferty Mar 01 '10 at 14:50
  • Yeah that happens, so isn't possible to raise on top of the alertdialog? – Pentium10 Mar 01 '10 at 15:09
  • 1
    I don't think the AlertDialog.Builder gives you that level of control. It is a convenience method for writing simple dialogs, if you need something more specific then usually you write your own dialog activity. – seanhodges Mar 01 '10 at 23:11
3

Had this problem myself as well, when I wanted to show a validation message within a dialog. The answer that seanhodges gave is probably the cleaner and better way. But a seperate activity wasnt practical for me, so i came up with this solution.

Anyway, you can use the AlerDialog.Builder, and show a toast. If you override the OnClickListener of the button the you want to trigger the toast, you can show a toast on top of a dialog.

An example;

public void showToastOnDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Dialog title");
    builder.setMessage("Dialog message");
    builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do nothing, you will be overriding this anyway
        }
    });
    builder.setNegativeButton(android.R.string.cancel,
            new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // You can implement code here, because you wont be
                    // overriding this
                }
            });
    final AlertDialog dialog = builder.create();
    // Make sure you show the dialog first before overriding the
    // OnClickListener
    dialog.show();
    // Notice that I`m not using DialogInterface.OnClicklistener but the
    // View.OnClickListener
    dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Toast toast = Toast.makeText(context,
                            "I`m a toast on top of a dialog.",
                            Toast.LENGTH_LONG);
                    toast.show();
                    // Because you are overriding the OnClicklistener, the
                    // dialog will not auto dismiss
                    // after clicking
                    dialog.dismiss();
                }
            });
}
S.Choo
  • 31
  • 3
0

Try this:

AlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
bguiz
  • 27,371
  • 47
  • 154
  • 243