javaandroidandroid-fragmentsandroid-activity

Android API < 35 full screen app confusion


I'm working on an Android app (home project) written in Java. I'm a casual Android developer and in order to achieve full-screen functionality I applied the same steps I successfully used couple of years ago:

  1. Change the theme:
<style name="Base.Theme.MyApp" parent="Theme.Material3.Light.NoActionBar">
    <item name = "android:windowActionBar">false</item>
    <item name = "android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowLightStatusBar">false</item>

</style>

<style name="Theme.MyApp" parent="Base.Theme.MyApp" />
  1. Main activity (extending FragmentActivity, the only activity I have since I'm using fragments):
protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         EdgeToEdge.enable(this); //apparently for API 35+ this is the only thing I need
 
         View decorView = getWindow().getDecorView();
         int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
         decorView.setSystemUiVisibility(uiOptions);
 
         setContentView(R.layout.activity_main);
         ... }

The problem is, when testing this on API 34, my app is kind of full screen. There's a thin black line on top where system bar should be. Also, Play Store gave me a warning I'm using deprecated methods for full screen view. In order to resolve that I went through Google documentation, realized there are different methods for API versions 29 - 35 (my min SDK supported is 25) but apparently I couldn't understand implementation properly as whenever I tried solutions from the Google docs - black line either stayed or app became full-screen with system bar drawn over top part of my app. I tried using insets and margins as Google docs suggested but it gave no results. I was so desperate I even asked GTP for the solution and, obviously, I got speculative and non-working "solutions" and "ideas".

Can you please help me with this? How to implement a proper full-screen support that works for API versions 29-35?

It's incredible such a trivial every-day thing as showing app in full screen mode with no other elements on the screen is so elaborate and requires more code than setting a mere property of the activity hahah

EDIT: This is the latest attempt I tried:

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        EdgeToEdge.enable(this);

        Window window = getWindow();
        View decorView = getWindow().getDecorView();

        WindowCompat.setDecorFitsSystemWindows(window, false);
        WindowInsetsControllerCompat controllerCompat = new WindowInsetsControllerCompat(window, decorView);
        controllerCompat.hide(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.navigationBars());
        controllerCompat.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);

and the result I get (top of the screen): enter image description here

enter image description here

I have also removed anything from my themes.xml, theme parent is just Theme.Material3.Light.NoActionBar


Solution

  • Ok, solved! For anyone having the same problem - I figured it out. So:

    I used an emulator without front and back cameras but when I switched the skin to Pixel 5 I realized that black line height perfectly corresponds to front camera height. I continued digging deeper and found out that using this code in "onCreate" method:

    decorView.setOnApplyWindowInsetsListener((v, insets) -> {
                return insets.consumeSystemWindowInsets(); // Consume all insets to avoid unexpected padding
            });
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                layoutParams.layoutInDisplayCutoutMode =
                        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                getWindow().setAttributes(layoutParams);
            }
    

    forces Android to extend the activity all the way, even if it means certain parts of the UI might be obscured by the front camera. Now it works! :)

    P.S. IDK why it's so hard for Android to understand when I say "full screen" with no other options specified, I want THE full screen even if it has consequences in the form of parts of hardware obstructing the UI... :)