44

I'm trying to create a Nav Drawer like the one from the Material spec (like the one from the new gmail app). Note how the contents of the nav drawer draw behind the status bar:

example from the Material spec

Using Chris Banes' answer from this question, I was able to successfully make the navigation drawer in my app draw behind the status bar; that's working fine. What isn't working is drawing the contents of the nav drawer behind the status bar. I want the blue image in my drawer to be displayed behind the status bar, but that area is drawn with the color of status bar, as seen in this screenshot.

my app

So, how can I make my navigation drawer draw in the area behind the status bar? I've posted the relevant parts of my project below.

Base layout containing the navigation drawer:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/warning_container" />

    <FrameLayout
        android:id="@+id/navigation_drawer_fragment_container"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start">

        <fragment
            android:id="@+id/navigation_drawer_fragment"
            android:name="com.thebluealliance.androidclient.fragments.NavigationDrawerFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_navigation_drawer" />

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

Theme of my activity

<style name="AppThemeNoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

In onCreate() of my activity, I do the following:

mDrawerLayout.setStatusBarBackground(R.color.primary_dark);
Community
  • 1
  • 1
Nathan Walters
  • 4,116
  • 4
  • 23
  • 37

6 Answers6

53

For API 21+

<style name="AppTheme" parent="android:Theme.Holo.NoActionBar.TranslucentDecor">
    ...
</style>

For API 19+

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

Your layout should have android:fitsSystemWindows="false" (which is the default).


Now since you want to toggle the translucency you can do it programatically:

Window window = getWindow();

// Enable status bar translucency (requires API 19)
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// Disable status bar translucency (requires API 19)
window.getAttributes().flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// Set a color (requires API 21)
window.setStatusBarColor(Color.RED);

I leave all the sdk version checking to you :)


ss

Simas
  • 43,548
  • 10
  • 88
  • 116
  • AppCompat doesn't seem to have a corresponding theme. – Nathan Walters Nov 16 '14 at 15:53
  • @NathanWalters what makes you think Gmail uses AppCompat? – Simas Nov 16 '14 at 16:17
  • 1
    The fact that they offer material design on versions of Android other than 5.0. It would be incredibly pointless to duplicate all the functionality from AppCompat when they already invested so much time in creating it in the first place. – Nathan Walters Nov 16 '14 at 16:35
  • @NathanWalters Well since I failed to get the screen you have shown on my gmail app, I've tried [Billy](https://play.google.com/store/apps/details?id=com.vibin.billy) on api 21 and api 17. One had a transparent status bar and the other a solid one. Have you seen a transparent status bar on a lower api or are you guessing? – Simas Nov 16 '14 at 16:40
  • What? I never said you could get transparent status bars on anything lower than 4.4. You asked why I thought they were using AppCompat, and I gave my reason. – Nathan Walters Nov 16 '14 at 16:47
  • Ah, OK. Is there a nice way to make the platform draw in the colored status bar or do we have to do that ourselves? – Nathan Walters Nov 16 '14 at 16:52
  • @NathanWalters I'm not sure what you meant by "make the platform draw in the status bar". Nobody is drawing in it or over it. It's just transparent and you can see the views that are underneath it. – Simas Nov 16 '14 at 16:56
  • In 5.0, the status bar should be colored. I was asking if there was a way to have the system color it for you when it is translucent. But, based on what you said, I'd have to place a view behind the status bar and color it myself. Is this correct? – Nathan Walters Nov 16 '14 at 16:59
  • 1
    @NathanWalters is there anything wrong with my answer? I does answer your question. – Simas Nov 17 '14 at 12:00
  • It'll sound stupid, but it doesn't answer it in the way I'd hoped. It looks like it should work for me; when I have time, I'll test it out. – Nathan Walters Nov 17 '14 at 12:10
  • This answer should be accepted. The new gmail has a transparent actionbar with a colored layout under it. The nav drawar slides in between them. – Lukos Nov 18 '14 at 20:36
  • I found the best way to do this with Android 5.0; see my new answer. However, I'll upvote yours because it could be useful for people on KitKat and you obviously put a lot of effort into it :) – Nathan Walters Nov 20 '14 at 23:54
17
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true">

        <include layout="@layout/toolbar" />
        <!-- Main layout -->
        <FrameLayout
            android:id="@+id/main_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <!-- Nav drawer -->
    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.example.DrawerFragment"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:fitsSystemWindows="true" />

</android.support.v4.widget.DrawerLayout>

values/themes.xml

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@color/primary</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
</style>

<style name="AppTheme" parent="AppTheme.Base">
</style>

values-v19/themes.xml

<style name="AppTheme" parent="AppTheme.Base">
    <!--This makes the status bar transparent in KK and Lollipop-->
    <!--You do not need values-v21 and if you create them make sure you extend from this one-->
    <item name="android:windowTranslucentStatus">true</item>
</style>

If you want to change the color (different from transparent black) of the status bar, you are gonna need to go for the other approach with the custom view, because mDrawerLayout.setStatusBarBackgroundColor(int) will only be activated if this DrawerLayout fitsSystemWindows (android:fitsSystemWindows="true"), and if you do it won't be draw behind the status bar but under it.

jpardogo
  • 5,636
  • 2
  • 23
  • 27
  • The problem with this and the official answer is that it doesn't color the status bar. The background of the first child (LinearLayout) is used instead of the provided color. – tomrozb Nov 24 '14 at 00:39
  • I modified the answer after some more test – jpardogo Nov 24 '14 at 11:42
  • ScrimInsetFrameLayout is necessary for Samsung devices because they don't paint the statusBar with the transparent black by default. So, to keep consistency across devices you need ScrimInsetFrameLayout. – jpardogo Apr 10 '15 at 09:57
11

I found the best way to do this on Android 5.0. The key is to use a ScrimInsetFrameLayout as the root element of the navigation drawer (the second View in the DrawerLayout). This will make the content expand to fill the space behind the status bar. To color the inset properly, you can set the following attribute on the ScrimInsetFrameLayout:

app:insetForeground="#4000"

Also, make sure that you have android:fitsSystemWindows="true" on the scrim layout!

The source code for the ScrimInsetFrameLayout can be found here: https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java

Nathan Walters
  • 4,116
  • 4
  • 23
  • 37
  • 3
    ScrimInsetFrameLayout is not needed. I don't know why this answer is accepted. – jpardogo Nov 21 '14 at 12:29
  • 1
    @jpardogo then what is the best way to do this? I assumed the way that Google itself designed their apps would be the most correct way – Nathan Walters Nov 21 '14 at 12:32
  • Lollipop wasn't released when this app was released. They way chris banes (DPE in the Android Developer Relations team) explained is the correct one. I know it doesn't work, but I answer this question with the modifications to make it work. You don't need custom views for this. It works natively. – jpardogo Nov 21 '14 at 12:56
  • 1
    No, Lollipop wasn't released, but neither was Material Design. This behavior came with an update to I/O. Anyway, i just tried your method and I got exactly the same thing I was getting before. – Nathan Walters Nov 21 '14 at 13:35
  • Up to you. The solution I posted works, I am actually using it. – jpardogo Nov 21 '14 at 17:16
  • 1
    Your solution gave me what i originally had: the nav drawer extended behind the status bar, but the content of the nav drawer didn't. I used your solution verbatim. – Nathan Walters Nov 21 '14 at 18:01
  • @jpardogo The official implementation is simply wrong, it doesn't respect the status bar color because background of the first child is used below the status bar. – tomrozb Nov 24 '14 at 00:42
  • I modified the answer after some more test – jpardogo Nov 24 '14 at 11:43
  • I think this answer have something interesting to look about. Take a look at the Google IO App source code here -> http://goo.gl/hIoQ3n Is using exactly this!! – Sotti Nov 24 '14 at 16:36
  • @Sotti that's exactly what I based my answer off of! – Nathan Walters Nov 24 '14 at 21:57
  • @NathanWalters have a solution for this one? http://stackoverflow.com/questions/29271251/put-navigation-drawer-under-status-bar – Andrew Quebe Mar 26 '15 at 15:14
1

For everybody struggling with a translucent statusBar in combination with a navBar, but unwilling to change <style name="yourAppTheme" parent= to "android:Theme.Holo.NoActionBar.TranslucentDecor", simply add these lines in your style.xml:

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item> 
    <item name="android:windowContentOverlay">@null</item>
luke8800gts
  • 398
  • 3
  • 7
0

In your directory "values-v21", add these lines:

<style name="AppTheme" parent="BaseTheme">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowSharedElementsUseOverlay">false</item>
</style>
Juan Labrador
  • 1,204
  • 10
  • 14
0

Just add these two values to your theme style in values-v21

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  ..
  <item name="android:statusBarColor">@android:color/transparent</item>
   <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
Kartik Sharma
  • 1,713
  • 2
  • 10
  • 5