Android Question Immersive Mode with notch? makes problems

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

i test this project.

But on my Device (Oneplus 6) the notch is black and under the statusbar ends the red line, with round corners, but under the black statusbar.

does anyone else have the problem? i have nothing change on the example project.
 

Semen Matusovskiy

Well-Known Member
Licensed User
Can't say about OnePlus 6, but on my OnePlus A5010 (Android 8.1.0, OxygenOS 5.1.5) everything fine.
"Camera" app also uses immersive mode. Do you see any troubles ?
 
Upvote 0

capisx

Member
Licensed User
Longtime User
Hi @Alexander Stolte,

Just add this following code to your theme file (Tested on Oneplus 6):
B4X:
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

Use Manifest Editor to do so.

How to add above code to manifest editor?

I've tried to add the code to manifest editor like below:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
But the results are still not right. (tested on Xiaomi MI 8)
Screenshot_2018-12-11-16-04-58-132_b4a.example4.png Screenshot_2018-12-11-16-05-11-914_b4a.example4.png
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
How to add above code to manifest editor?

I've tried to add the code to manifest editor like below:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
But the results are still not right. (tested on Xiaomi MI 8)
View attachment 75210 View attachment 75211
Add it to your theme like this:
B4X:
SetApplicationAttribute(android:theme, "@style/CustomTheme")

CreateResource(values, theme.xml,
<resources>
    <style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
</resources>
)
 
Upvote 0

capisx

Member
Licensed User
Longtime User
Add it to your theme like this:
B4X:
SetApplicationAttribute(android:theme, "@style/CustomTheme")

CreateResource(values, theme.xml,
<resources>
    <style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
</resources>
)

Thanks Brandsum for your reply, but i get this error after add your code to manifest editor:
B4X:
Generating R file.    Error
res\values\theme.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.NoActionBar'.
res\values\theme.xml:4: error: Error: No resource found that matches the given name: attr 'android:windowLayoutInDisplayCutoutMode'.

Did i miss something?
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
Thanks Brandsum for your reply, but i get this error after add your code to manifest editor:
B4X:
Generating R file.    Error
res\values\theme.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.NoActionBar'.
res\values\theme.xml:4: error: Error: No resource found that matches the given name: attr 'android:windowLayoutInDisplayCutoutMode'.

Did i miss something?
Use latest android.jar and add this following code to your activity if you want to use AppCompat.
B4X:
#Extends: android.support.v7.app.AppCompatActivity
You can change Theme.AppCompat.Light.NoActionBar as per your need. Like Theme.holo.light

NOTE: android:windowLayoutInDisplayCutoutMode
provided by the APIs in Android Pie 9.0.
 
Upvote 0

capisx

Member
Licensed User
Longtime User
Use latest android.jar and add this following code to your activity if you want to use AppCompat.
B4X:
#Extends: android.support.v7.app.AppCompatActivity
You can change Theme.AppCompat.Light.NoActionBar as per your need. Like Theme.holo.light

NOTE: android:windowLayoutInDisplayCutoutMode
provided by the APIs in Android Pie 9.0.

I have followed the instructions you gave and the error was gone but the results are still the same as the screenshots I posted above. there are no changes.
It seems like the immersive mode code return the wrong screen size (in the screenshots the red frame rectangle pushed out of screen by the notch bar).

I have tried the first post project and my project using Xiaomi MI 8 with same result.

Maybe i still missing something.
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
Use this code:

B4X:
Sub GetRealSize As LayoutValues
    Dim lv As LayoutValues
    Dim p As Phone
    If p.SdkVersion >= 17 Then
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim display As JavaObject = ctxt.RunMethodJO("getSystemService", Array("window")).RunMethod("getDefaultDisplay", Null)
        Dim point As JavaObject
        point.InitializeNewInstance("android.graphics.Point", Null)
        display.RunMethod("getRealSize", Array(point))
        lv.Width = point.GetField("x")
        lv.Height = point.GetField("y")
    Else
        lv.Width = 100%x
        lv.Height = 100%y
    End If
    lv.Scale = 100dip / 100
    Return lv
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim jo As JavaObject = Activity
    jo.RunMethod("setSystemUiVisibility", Array As Object(5894)) '3846 - non-sticky
    Dim lv As LayoutValues = GetRealSize
    Dim jo As JavaObject = Activity
    jo.RunMethod("setBottom", Array(lv.Height))
    jo.RunMethod("setRight", Array(lv.Width))
    Activity.Height = lv.Height
    Activity.Width = lv.Width

    Activity.LoadLayout("Layout1")
End Sub
 
Upvote 0

capisx

Member
Licensed User
Longtime User
I've tried your code @Brandsum but it still not working on my Xiaomi MI 8 (Android 8), but when i tried on Genymotion with Android 9 + simulated top notch and its working for the shortEdges, but return the wrong display size if i change the value to "default"/"never" like the screenshots.

How do i get the correct display size after black bar that cover the notch added?
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
not working on my Xiaomi MI 8 (Android 8), but when i tried on Genymotion with Android 9 + simulated top notch and its working for the shortEdges

That is quite natural as I mentioned earlier.
NOTE: android:windowLayoutInDisplayCutoutMode provided by the APIs in Android Pie 9.0.

but return the wrong display size if i change the value to "default"/"never" like the screenshots
As per this documentation https://developer.android.com/guide/topics/display-cutout/
If you use never then the content never renders into the cutout area. And if you use default then the content renders into the cutout area as per system default settings. So if you use default you might have to set the app to fullscreen mode in device settings.

How do i get the correct display size after black bar that cover the notch added?
If you use never and want to get the correct display height without that notch covered area then you have to deduct the notch height from the actual height.
Check this to get the notch area height: https://developer.android.com/reference/android/view/WindowInsets#getDisplayCutout()
 
Upvote 0

capisx

Member
Licensed User
Longtime User
If you use never and want to get the correct display height without that notch covered area then you have to deduct the notch height from the actual height.
Check this to get the notch area height: https://developer.android.com/reference/android/view/WindowInsets#getDisplayCutout()

Thanks alot @Brandsum for pointing me to the right direction, but I am very blind with java.

Could you or anyone here can help me to modify this project to get the correct display size if the phone has the notch?
 
Upvote 0
Top