3

I am trying to have a blurry background behind an AlertDialog. The process goes like the following: take a screenshot of the activity and blur it and save it to a bitmap. Then converting this bitmap to a drawable. Then I set the drawable background of the window of the alert using this line of code renameDialog.getWindow().setBackgroundDrawable(draw); The issue is that the dialog is only getting shown on the top of the screen even though I am forcing a certain location by specifying a layout parameters. I noticed if I changed setBackgroundDrawable to setBackgroundDrawableResource and give it an existing drawable, then the dialog is shown at the specified location.

What needs to be done so that the setBackgroundDrawable can be used with a specified location to the dialog?

Thanks!

        AlertDialog renameDialog = new AlertDialog.Builder(this).create();

        LayoutInflater factory = LayoutInflater.from(this);
        deleteDialogView = factory.inflate(R.layout.dialog_rename_playlist, null);

        //Getting Blurry screenshot to display it behind dialog
        Bitmap backgroundScreenshot = Utils.takeScreenShot(PlaylistActivity.this);
        Bitmap blurryBackground = Utils.getBlurredBitmap(backgroundScreenshot, 30);

        WindowManager.LayoutParams wmlp = renameDialog.getWindow().getAttributes();
        wmlp.gravity = Gravity.CENTER_VERTICAL;
        wmlp.x= 100;
        wmlp.y= 100;


        final Drawable draw=new BitmapDrawable(getResources(), blurryBackground);
        renameDialog.getWindow().setBackgroundDrawable(draw);

        renameDialog.getWindow().setAttributes(wmlp);
        renameDialog.setView(deleteDialogView, 0, 0, 0, 0);
        renameDialog.show();
Ziad Halabi
  • 964
  • 11
  • 31

3 Answers3

1

By setting the window's background to a screen-sized drawable, you're making the window grow to full-screen dimensions. In this case, Gravity.CENTER_VERTICAL cannot do anything. Calculating with Gravity.CENTER_VERTICAL works something like this:

spacingAtTop = spacingAtBottom = (containerHeight - contentHeight) / 2

After setting the background, contentHeight is equal to containerHeight.

If possible, post R.layout.dialog_rename_playlist.

One thing you can try without changing much:

Wrap the content of R.layout.dialog_rename_playlist inside a FrameLayout with height set to match_parent. The current parent(call it oldParent) of R.layout.dialog_rename_playlist should then have its height set to wrap_content and layout_gravity set to center_vertical. This should at least bring your content to the center. But it won't fix your positioning issue.

A more robust approach would be to use FrameLayout as the parent container for your Activity/Fragment. before displaying the dialog, take a screenshot, blur it, and set it to this FrameLayout's foreground. Make your AlertDialog's background transparent, and you should get the same effect.

Vikram
  • 51,313
  • 11
  • 93
  • 122
0

Bunch of guesses

wmlp.gravity = Gravity.CENTER_VERTICAL; //put me in the middle vertically
wmlp.x= 100; //sorry i have no effect
wmlp.y= 100;//dude!! you are wasting lines; i have no effect

do this

wmlp.gravity = Gravity.FILL; // i grow up when needed
wmlp.x= 100; // now i am useful
wmlp.y= 100; //dude!! you are wasting lines; use wmlp.y = wmlp.x = 100

Hope it helps

Elltz
  • 10,730
  • 4
  • 31
  • 59
0

I think you can use PopupWindow

Ashish Singh
  • 371
  • 3
  • 16