I have a app's toolbar issue on Android API 30+. The device's status bar overlap my app's toolbar and the toolbar is gone. This problem does not occur on device with API below 30 (29, 28, 27, ...). I searched through Stackoverflow and did not found any working solution for my case.
I tried getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
it brings the toolbar back and show the app in full-screen mode but the device's status bar and bottom navigation bar overlap (half transparent) the app's content.
What I want is the devices's status bar and bottom navigation bar are not overlap with the app's content. They can appear or disappear.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main content -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<include
android:id="@+id/toolbar"
layout="@layout/toolbar_main"/>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar" />
<!-- Loading box -->
<RelativeLayout
android:id="@+id/boxProgress"
android:layout_width="@dimen/loading_box_size"
android:layout_height="@dimen/loading_box_size"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:visibility="gone">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:alpha="0.8"
android:background="@drawable/background_round_corner" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/box_margin_medium"
android:indeterminate="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/progressBar"
android:layout_centerHorizontal="true"
android:text="@string/label_status_loading"
android:textSize="@dimen/text_size_medium" />
</RelativeLayout>
</RelativeLayout>
<!-- Order drawer (slide from right) -->
<FrameLayout
android:id="@+id/drawer"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="end" />
</androidx.drawerlayout.widget.DrawerLayout>
toolbar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="36dp"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
android:contentInsetStartWithNavigation="0dp"
android:fitsSystemWindows="true"
android:minHeight="36dp"
android:theme="@style/ToolbarStyle"
android:layout_marginBottom="@dimen/box_margin_bottom_small"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageView
android:id="@+id/toolbarIndicator"
android:layout_width="36dp"
android:layout_height="36dp"
android:scaleType="centerInside"
android:padding="@dimen/box_padding_small"/>
<FrameLayout
android:id="@+id/toolbarContainer"
android:layout_width="match_parent"
android:layout_height="36dp" />
</androidx.appcompat.widget.Toolbar>
Application Theme
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">...</style>
I finally got it works!! In the past I used to add the following code to onCreate()
in MainActivity.kt.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
this.window.setDecorFitsSystemWindows(false)
}
After I removed it, everything is back to what it should be. Thank you C.F.G for trigger me about fitSystemWindows
property.