Cannot fill background of display cutout on landscape with desired color, looks ugly on Huawei

Viewed 101

I developed app and thought everything is right with full screen until I run it on huawei cloud debugging real devices and discovered it is not working there. I tried everything and don't see much about it anywhere. It looks like I cannot get system bar space when running on landscape to disappear.enter image description here

I can see similar behaviour on all huawei phones.

My original approach was like this:

val window = activity().window
val controller = WindowInsetsControllerCompat(window, view)
controller.systemBarsBehavior = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller.hide(systemBars())

Then I started trying anything I found on SO.

Added to theme:

<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>

Before setContentView in Activity onCreate added:

activity().requestWindowFeature(Window.FEATURE_NO_TITLE)

and bunch of other crazy or deprecated stuff like:

window.addFlags(FLAG_FULLSCREEN)
window.addFlags(FLAG_LAYOUT_IN_SCREEN)
window.addFlags(FLAG_LAYOUT_NO_LIMITS)
window.addFlags(FLAG_LAYOUT_IN_OVERSCAN)

    window.decorView.systemUiVisibility =
        SYSTEM_UI_FLAG_LAYOUT_STABLE or SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                SYSTEM_UI_FLAG_FULLSCREEN or SYSTEM_UI_FLAG_IMMERSIVE

Edit: After some more evaluation it looks I actually have full screen right but that is "just" a space behind notch that for whatever reason doesn't match color of my application background. So I was playing also with themes if I cannot change some background color so I will change color of that notch background space.

This are my styles that doesn effect notch:

<item name="android:windowBackground">?attr/inst_surface</item>
<item name="colorSurface">?attr/inst_surface</item>
<item name="colorPrimary">?attr/inst_surface</item>
<item name="colorPrimaryVariant">?attr/inst_surface</item>
<item name="colorPrimaryDark">?attr/inst_surface</item>
<item name="android:statusBarColor">?attr/inst_surface</item>
<item name="colorOnSurface">?attr/inst_text_strong</item>
<item name="colorSecondary">?attr/inst_control_active_foreground</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>

I also tried this:

window.clearFlags(FLAG_TRANSLUCENT_STATUS);
window.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.statusBarColor = ContextCompat.getColor(activity(), cs_white);
1 Answers

So look like there is official solution for notches I was not aware and I have to implement it in theme.

https://developer.android.com/guide/topics/display-cutout

Looks like in landscape you cannot color background when using landscape orientation so solution could be more work actually, I have to cover whole area adjust layout so I have desired color there.

So

<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

is partly a solution but great amount of work is needed to make margin for any layout that needs it. Also as I understand notch can be on both sides so you have to use WindowInsetsCompat.getDisplayCutout() to determine where it is and adjust layout to stay out of way in code. If anyone know easier solution would be nice.

Edit:

So top level final code looks like this:

onOrientationChange {
    navigation.view.updateLayoutParams<MarginLayoutParams> {
        leftMargin = displayCutout?.leftRect?.right ?: 0
        rightMargin = displayCutout?.rightRect?.width ?: 0
    }
}

Small explanation: onOrientationChange uses OrientationEventListener and defaultDisplay.rotation and also viewTreeObserver.addOnGlobalLayoutListener to determine rotating of display, this was the hard part, then I just for now implemented it easily in top level of my application, looks pretty well now on simulator when testing display cutout using developer options.

enter image description here

enter image description here

I will release solution in my Renetik Android UI library.

Last thing to consider is to disable windowTranslucentStatus, windowTranslucentNavigation for api 26 otherwise it crashes :/ like this:

<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>

in values-v26 folder

Related