How to add LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES in Window Manager Params?

3k views Asked by At

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;
1

There are 1 answers

3
aspix On BEST ANSWER

Try setting in your Activity:

window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES