How to add LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES in Window Manager Params?
Asked Answered
E

1

6

I am creating an Android App in which I am showing a floating view on top of all apps by using a service and Window Manager. I am trying to add WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES in my Window Manager params to enable my layout to overlay with display cutouts. But it seems like this is not working in my case.

I have already tried to add it in both ways:

  1. By using XML styling:

    <style name="OverlayTheme">
      <item name="android:windowLayoutInDisplayCutoutMode">
        shortEdges <!-- default, shortEdges, never -->
      </item>
    </style>
    

    And added this style in my layout root view. But it does not work.

  2. By using the JAVA code in my service:

    params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                        PixelFormat.TRANSLUCENT);
    

    It also not working. I am not sure if I am applying the Layout Params Correctly or not. Please help me to figure out the problem. Thanks in advance.

Edit:

This is the way I had implemented my the bubble in my service:

  1. Initialized Window Manager and Layout in onCreate() method:

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (inflater != null)  mLayout = inflater.inflate(R.layout.my_layout, null, false); 
    
  2. Added this view to window by Window Manager:

    WindowManager.LayoutParams params;
    params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                        PixelFormat.TRANSLUCENT);
    
  3. Finally added this view to Window by addView Method:

    windowManager.addView(mLayout, params);
    

    By using the above methods I am able to show my view on top of other apps. But the problem is that my view is not not overlapping with a display cutout. This is happening when android is in landscape mode.

Solution:

I solved this by just adding this line of code (As suggested in Answere):

     params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
Exorcism answered 13/10, 2019 at 6:4 Comment(0)
H
11

Try setting in your Activity:

window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
Highlander answered 13/10, 2019 at 8:7 Comment(3)
Thanks for your reply. But I am currently in service and not in any activity and I am setting my views on top of other apps (like a bubble) by using WindowManager.LayoutParams params and windowManager.addView(mLayout, params); method. I have updated the method in my Question so that you can better understand. As I have no reference for the window variable, I can't implement it like this. Tell me if I am wrong somewhere...Exorcism
Thank you so much. I managed to do it by setting params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; as suggested by you and it worked.Exorcism
Thank you so much for finding this! I want to add a caveat here that I was only able to get this working once I set the flag via this method programmatically (layoutInDisplayCutoutMode -- not via the setFlags parameter) before adding the view to the window. Thank you!!!Fantail

© 2022 - 2024 — McMap. All rights reserved.