2

I am trying to show an activity as a floating window with a dimmed background and I'm using the following code to do that, which is taken from the Google I/O 2016 project:

    protected void setupFloatingWindow(int width, int height, int alpha, float dim) {
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.width = getResources().getDimensionPixelSize(width);
    params.height = getResources().getDimensionPixelSize(height);
    params.alpha = alpha;
    params.dimAmount = dim;
    params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    getWindow().setAttributes(params);
}

I am calling this function in onCreate, before super.onCreate and setContentView , as it is in the Google I/O example code.

This way it doesn't work for me. The activity is shown as a floating window, with the size that I am setting, but the background behind is black and not dimmed as I expect it to be. The dimAmount that I am setting is 0.4f.

Am I missing something? Should I add something more? Please help

Sandra
  • 4,239
  • 10
  • 47
  • 80

1 Answers1

1

Two things to try:

The params.alpha value is a float that should be between 0.0 and 1.0, but you are passing an int, so the alpha value gets set to fully opaque or fully transparent, nothing in between.

Also, I was only able to get this working on my emulator after changing the activity style to include:

<item name="android:windowIsTranslucent">true</item>

I don't know if that's set in the project you are working from or not.

Jason Powell
  • 361
  • 4
  • 12
  • It was not the alpha value. You are right, after browsing the Google I/O application I found the solution that you suggested, the second one. So setting android:windowIsTranslucent to true did the trick for me. In the Google I/O application they also set android:windowContentOverlay to @null. I don't know if that makes some kind of difference, but I set that as well. – Sandra Apr 23 '17 at 08:56