185

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?

overlapping toolbar

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>
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
emen
  • 6,050
  • 11
  • 57
  • 94
  • How did you customize your `Theme` of your applicaiton? post them. – SilentKnight Apr 20 '15 at 02:46
  • in `CoordinatorLayout` you can try this: stackoverflow.com/a/47906087/2201814 – MHSaffari Dec 23 '17 at 06:14
  • for those who are facing the issue with androidx.coordinatorlayout.widget.CoordinatorLayout as root view should set android:fitsSystemWindows="false" .this worked for me... as suggested by one of SO user below.. – Muahmmad Tayyib May 15 '20 at 16:02

10 Answers10

301

Use android:fitsSystemWindows="true" in the root view of your layout (LinearLayout in your case).

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sohaib
  • 10,941
  • 9
  • 32
  • 34
  • Thanks. This works. But I was wondering if there is a way to do this globally? – emen Apr 20 '15 at 02:49
  • 23
    @AimanB you can set it globally by setting in the theme as `true` – Samir Jun 17 '15 at 12:08
  • 13
    @Samir this has a side-effect as reported in [this comment](http://stackoverflow.com/questions/20578780/taking-advantage-of-the-translucent-status-bar-in-android-4-4-kitkat#comment32445193_21130544) – Sufian Sep 02 '15 at 13:12
  • By default, your application should respect the system windows. You might open your activity with some flags perhaps ? – stdout Apr 14 '16 at 11:54
  • 10
    This issue will come even after we added android:fitsSystemWindows="true" to root layout, If your style contains @android:color/transparent this. Just mentioned for those who are reading this question. – Sakkeer Hussain May 27 '16 at 15:48
  • 5
    I'm having same issue and the android:fitsSystemWindows="true" not worked for me. Any one has solutions this please post. – Jatin Jun 08 '16 at 07:38
  • @Jatin using android:fitsSystemWindows="true" in those layouts where you are facing this issue should solve it just solve mine. – mfaisalhyder Jun 10 '16 at 05:53
  • @MFaisalHyder I've navigation drawer and added to there and also in my header layout too.. still issue not solving. – Jatin Jun 10 '16 at 06:13
  • @Jatin add this property in only that layout in which toolbar is overlapping status bar and specific values-v1X folder like if you're testing on 5.0 then add this in styles of values-v1X accordingly @color/ColorPrimaryToolBar true – mfaisalhyder Jun 10 '16 at 06:19
  • 1
    This answer fixed my problem. But now I find all android toasts with 0 padding! – kds23 Oct 24 '16 at 16:48
  • I can confirm that in activity_main.xml inside android.support.v4.widget.DrawerLayout you shoud write `android:fitsSystemWindows="true"`. – CoolMind Nov 30 '16 at 17:50
  • Not working in root layout but worked in app style. **android:fitsSystemWindows="true"** – Lalit Sharma Jul 25 '17 at 06:26
  • 1
    Not working at all. This is the worst nightmare ever. Cmon Android there's only two properties to handle: background color and height of the statusbar and navigation bar. Is that really that hard ? – ozapa Aug 09 '17 at 15:28
  • 2
    This worked like a charm but I had to remove `android:fitsSystemWindows` in toolbar itself. – Pei Apr 17 '18 at 01:16
  • android:fitsSystemWindows="true" ignores padding in root layout so views inside it hit display edges. Margins in root are honoured but grey borders appear. It is material_grey_850 which I believe appears because of using Theme.AppCompat. Annoying things also happen in one app with the normally hidden navigation bar. In emulator when navigation bar pops up it squashes the single view I have with layout weight while in one hardware bottom of root layout rests where nav bar will appear, regardless if nav bar is hidden. Just testing in old app but interesting to see issues arise which need fixing. – Smartybartfast Jun 29 '18 at 07:59
19

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>
Zahra.HY
  • 1,684
  • 1
  • 15
  • 25
12

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

Garg
  • 2,731
  • 2
  • 36
  • 47
anoop ghildiyal
  • 821
  • 10
  • 18
11

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.

Salix alba
  • 7,536
  • 2
  • 32
  • 38
Muz
  • 5,866
  • 3
  • 47
  • 65
10

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>
inder_gt
  • 492
  • 1
  • 7
  • 9
6

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>
1'hafs
  • 559
  • 7
  • 25
3

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>
Abhishek Garg
  • 3,092
  • 26
  • 30
2

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>
Lavanya Velusamy
  • 333
  • 1
  • 6
  • 16
2

To prevent the Toolbar from overlapping the status bar, add these lines to your theme

<item name="android:fitsSystemWindows">true</item>
<item name="android:windowTranslucentStatus">true</item>

it worked for me

Darshan
  • 4,020
  • 2
  • 18
  • 49
1

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.