Search code examples
javaandroidkotlin

How adjust my app in Android 11 in cell phone screen?


Before Android 11, I've adjusted my app to fullscreen easily

My old phone had the camera hole and base buttons outside the screen area, my new phone has a camera hole and the base buttons inside a screen.

With few settings, my app was fullscreen in the old phone.

Styles.xml

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>   
        <item name="windowNoTitle">true</item>    
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
</style>

AndroidManifest.xml

<application
   ...       
   android:theme="@style/AppTheme.NoActionBar">
</application>

In my new phone with Android 11, I've searched many Stackoverflow question. I've tried many different solutions. In the end, I put in my app the following code in that start of OnCreate().

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  window.attributes.layoutInDisplayCutoutMode =
      WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    WindowCompat.setDecorFitsSystemWindows(window, false)
 else {
      @Suppress("DEPRECATION")
      window.setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN
      )
    }

I wanted a narrow margin near the base buttons and the camera hole, like Chrome.

But all can I get is a wide margin between the app and button (downward) and between the app and the camera hole (upward):

App 1

But I would want a narrow margin:

Ideal

similar to Chrome browser for Android:

My wish

How can I do this programatically?

I haven't been able to find a single clue to my problem in Internet (and StackOverflow)

I also haven't been able to figure out how to identify if a given cell phone has the camera hole on the screen or if it has the base buttons on the screen. It looks like it's based on DisplayCutout, WindowInsets and boundingRectTop and boundingRectBottom, but there is no real and clear usage example in Internet.


Solution

  • The relevant line is:

          <meta-data android:name="android.max_aspect" android:value="2.4"/>
    

    2.4 is maximum screen height / screen width allowed for the app area in the screen.

    If you omit this statement, the system still assumes that the maximum aspect ratio is 1.86.

    I don't know why the system assumes a default so low, if many devices today has a ratio screen height /screen width greater than 1.86.

    From what I've written inside the question, one really needs to change one of the standard styles of Android

    So in styles.xml:

    <style name="AppTheme"> parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>   
        <item name="android:windowFullscreen">true</item>
    </style>
    

    In AndroidManifest.xml includes

      <application
        ... 
        android:theme="@style/AppTheme">
        <meta-data android:name="android.max_aspect" android:value="2.4"/>
        ...  
       <\application>  
    

    It's all...

    The only remote reference I found on this subject was on the official blog of developers on a 2017 page in this post.