Search code examples
javaandroidandroid-sdk-2.1toast

How to raise a Toast on top of a AlertDialog?


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

Solution

  • 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.