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.
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
}