Toolbar overlapping below status bar
AndroidXmlAndroid LayoutMaterial DesignMaterial Components-AndroidAndroid Problem Overview
I want to have appcompat v21 toolbar in my activity. But the toolbar I'm implementing is overlapping below status bar. How can I fix it?
Here is the activity layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Toolbar view:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
Theme style:
<style name="AppTheme" parent="MaterialNavigationDrawerTheme.Light.DarkActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
Android Solutions
Solution 1 - Android
Use android:fitsSystemWindows="true"
in the root view of your layout (LinearLayout in your case).
And android:fitsSystemWindows
is an
> internal attribute to adjust view layout based on system windows such > as the status bar. If true, adjusts the padding of this view to leave > space for the system windows. Will only take effect if this view is in > a non-embedded activity. > > Must be a boolean value, either "true" or "false". > > This may also be a reference to a resource (in the form > "@[package:]type:name") or theme attribute (in the form > "?[package:][type:]name") containing a value of this type. > > This corresponds to the global attribute resource symbol > fitsSystemWindows.
Solution 2 - Android
Just set this to v21/styles.xml file
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
and be sure
<item name="android:windowTranslucentStatus">false</item>
Solution 3 - Android
For me, the problem was that I copied something from an example and used
<item name="android:windowTranslucentStatus">true</item>
just removing this fixed my problem.
Solution 4 - Android
None of the answers worked for me, but this is what finally worked after I set:
android:fitsSystemWindows="false"
In parent activity layout file it's not suggested at many places but it's work for me and saves my day
Solution 5 - Android
None of the answers worked for me, but this is what finally worked after i set android:fitSystemWindows
on the root view(I set these in styles v21):
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">false</item>
Make sure you don't have the following line as AS puts it by default:
<item name="android:statusBarColor">@android:color/transparent</item>
Solution 6 - Android
I removed all lines mentioned below from /res/values-v21/styles.xml and now it is working fine.
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="windowActionBar">false</item>
<item name="android:windowDisablePreview">true</item>
<item name="windowNoTitle">true</item>
<item name="android:fitsSystemWindows">true</item>
Solution 7 - Android
According google docs ,we should not use fitsSystemWindows attribute in app theme, it is intended to use in layout files. Using in themes can causes problem in toast messages .
Check Issue here & example of problem caused here
<item name="android:fitsSystemWindows">true</item>
Example of using correct way and which works fine with windowTranslucentStatus as well.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@color/white"
android:animateLayoutChanges="true"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/navigation_drawer_menu" />
</android.support.v4.widget.DrawerLayout>
Solution 8 - Android
Remove below lines from style or style(21)
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowTranslucentStatus">false</item>
Solution 9 - Android
There are so many reason to happen this.. firstly you must check your them how it was going on..
<style name="AppTheme" parent="BaseAppTheme">
<item name="android:windowBackground">@android:color/white</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorHint">@color/secondary_text</item>
</style>
if you use <item name="android:windowTranslucentStatus">true</item>
this code in your theme it should be overlap your action bar. you can use it false for solving this problem.
<item name="android:windowTranslucentStatus">false</item>
.
or if you want to keep it as true please use
<item name="android:fitsSystemWindows">true</item>
as true. You have another option to use this. you can use android:fitsSystemWindows="true"
this inside the root view of particular xml file.
Solution 10 - Android
To prevent toolbar overlapping the status bar add these lines in your theme
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindow">true</item>
it worked for me