The method getWindow() is undefined for the type AlertDialog.Builder

Bill Mote picture Bill Mote · Dec 16, 2011 · Viewed 12.5k times · Source

Idea taken from Android: Blurring and dimming background windows from dialog. I'm having trouble getting the content under my dialog to blur. When calling eula.getWindow() I receive this error:

The method getWindow() is undefined for the type AlertDialog.Builder

The eula is displayed with this bit of code from the main activity:

    EulaHelper.showEula(false, this);

Any help is greatly appreciated.

    public static void showEula(final boolean accepted, final FragmentActivity activity) {
    AlertDialog.Builder eula = new AlertDialog.Builder(activity)
            .setTitle(R.string.eula_title)
            .setIcon(android.R.drawable.ic_dialog_info)
                .setMessage(activity.getString(R.raw.eula))
            .setCancelable(accepted);

    if (accepted) {
        // If they've accepted the EULA allow, show an OK to dismiss.
        eula.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
    } else {
        // If they haven't accepted the EULA allow, show accept/decline buttons and exit on
        // decline.
        eula
                .setPositiveButton(R.string.accept,
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                setAcceptedEula(activity);
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton(R.string.decline,
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                activity.finish();
                            }
                        });
    }
    eula.show();
    WindowManager.LayoutParams lp = eula.getWindow().getAttributes();
    lp.dimAmount = 0.0F;
    eula.getWindow().setAttributes(lp);
    eula.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

}

Answer

user658042 picture user658042 · Dec 16, 2011

getWindow() is a method of the dialog class, not of the dialog builder. Your code should rather look like this:

AlertDialog dlg = eula.show();
WindowManager.LayoutParams lp = dlg.getWindow().getAttributes();
lp.dimAmount = 0.0F;
dlg.getWindow().setAttributes(lp);
dlg.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

Note though that the FLAG_BLUR_BEHIND constant is deprecated now, blurring behind windows is no longer supported. So your code might break in the future.