0

I am trying to make a full screen without system bars as my splash screen but I have some problems with resizing my root layout when my application starts.

I do not have any problem with a bottom because my root layout resizes behind the navigation bar and goes to the end of the screen what I actually wanted to do. The problem is with the top side of the root layout because it doesn't go to the top of the screen. On the top of my screen, I have black empty space which is left after I hide the status bar.

NOTE: Im using minSdkVersion 22 and targetSdkVersion 29.

For demonstration purposes, I will use simple code with one activity and one corresponding XML layout file with green background and 2 buttons within the Constraint Layout as root layout.

MainActivity:

package com.systemwindowexercise

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        window.navigationBarColor = resources.getColor(R.color.translucent)
        window.statusBarColor = resources.getColor(R.color.translucent)

        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)

    }

}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_light"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

themes:

<style name="Theme.SystemWindowExercise" parent="Theme.MaterialComponents.DayNight.NoActionBar">

On the left is the app before I touched the screen with black space without system bars, but with wrong resized root layout(green color), and on the right is the app after I touched the screen with system bars and correct resized root layout.

enter image description here

I want an app with a full screen without black space and with a full resized root layout and hid system bars for my splash screen before I do any interaction with the screen. How can I do that?

UPDATE: I'm using minSdkVersion 22 and targetSdkVersion 29 and I tested on Samsung Galaxy A40 (Android 10, API 29). On Samsung, I always get a black space on the top but on ZTE Nubia (API 22) all works fine. The only solution until now is to change device settings like this: How to Enable Full Screen Apps on Galaxy what @SlothCoding showed me in the comment.

UPDATE2: If you do not want to change in device settings then you need this code for phones which have a camera built in the screen area or in official terms you need it to display content in cutouts area:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
    }
Dezo
  • 835
  • 9
  • 16
  • 1
    Check this question: https://stackoverflow.com/questions/56353716/how-to-remove-top-status-bar-black-background – SlothCoding Jan 03 '21 at 21:30
  • I tested on a real device with API 29 and seems this app working with a full screen only if I change to a full-screen app in device settings. I also tested with API 22 and the app working with a full screen with my code without changing in device settings. – Dezo Jan 04 '21 at 12:19
  • I'm wondering if there a code to fix that without messing with device settings? – Dezo Jan 04 '21 at 12:24
  • Check this for documentation: https://developer.android.com/training/system-ui/immersive ... Also check this answer, maybe this could do that: https://stackoverflow.com/a/63446115/14759470 – SlothCoding Jan 04 '21 at 12:31

2 Answers2

0
<style name="Theme.xxx" parent="Theme.MaterialComponents.DayNight.NoActionBar">    //no actionbar, but status bar exists
    <item name="android:windowFullscreen">true</item>                       //remove status bar

Result:

enter image description here

Sam Chen
  • 7,597
  • 2
  • 40
  • 73
  • It did not work on a device with API 29, I still get black space on the top. – Dezo Jan 04 '21 at 13:12
  • @Dezo Check my updated picture, it works perfectly for me (Android 10). Try to comment out your logic code and run it again. – Sam Chen Jan 04 '21 at 15:42
  • I tried again and did not work on the Samsung galaxy A40 (android 10) because I still get a black space on the top. For now, I think only works this: https://www.gottabemobile.com/how-to-enable-full-screen-apps-on-galaxy-s10/ Maybe this works on other phones but not on Samsung galaxy A40 – Dezo Jan 04 '21 at 16:34
0

For hiding a status bar, navigation bar in your app and for not showing when user interact with the screen:
First, go to your res/theme (change it also in night mode):

<style name="Theme.xxx" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@android:color/transparent</item>
        <!-- Customize your theme here. -->
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
</style>

Next, go into your your first screen launch activity (mine is the Main Activity), create this function:

@SuppressLint("RestrictedApi")
fun createFullScreen(context: Context) {
        val window = getActivity(context)?.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window?.apply {
                setDecorFitsSystemWindows(false)
            }
        } else {
            window?.apply {
                setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
                )
                decorView.apply {
                    systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                            View.SYSTEM_UI_FLAG_FULLSCREEN or 
                            
                }
                statusBarColor =
                    ContextCompat.getColor(context, android.R.color.transparent)
            }
        }
    }

If you want to make fullscreen on the android device greater than API 30 (Android R), just do this and follow the same step:

@SuppressLint("RestrictedApi")
fun createFullScreen(context: Context) {
        val window = getActivity(context)?.window

        window?.apply {
            setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
            decorView.apply {
                systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                        View.SYSTEM_UI_FLAG_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            }
            statusBarColor =
                ContextCompat.getColor(context, android.R.color.transparent)
        }
    }

And finnally, just put it into the onCreate() function like this:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var adapterViewPager: PosterViewPagerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
//      Here for your called function
        createFullScreen(this@MainActivity)
}
  • 1
    I upgraded my android studio to API 30 and your code did not work on a device with API 29 but it worked on a device with API 22. This means on my Samsung A40 with API 29 I still have black space on the top. – Dezo Jan 04 '21 at 13:05
  • @Dezo I updated the code you just need to remove my if-else to check the api in the function createFullScreen(). – Nguyễn Hoàng Ân Jan 04 '21 at 13:36
  • Still get black space on the top. Maybe is a problem with Samsung Galaxy Axx phones. – Dezo Jan 04 '21 at 17:08