6

I have a problem where the status bar will not turn translucent. The translucent status bar and navigation bar code works fine by itself, however, when I implement the Google Maps Fragment into the activity view, the translucent bar turns gray. I have tested this with a fully transparent status bar but which again works at first, then has a white background once maps is added. It seems as if Google Maps has some sort of overlay. Any help is appreciated to get status bar to be translucent with Google Maps API.

As

XML File

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.mapwithmarker.MapsMarkerActivity"
        tools:layout="@layout/activity_main" />

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

Translucent status bar

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
Lord Goderick
  • 965
  • 2
  • 14
  • 32

3 Answers3

5

I have found the solution. Set android:fitsSystemWindows in DrawerLayout to "false", if anyone is adding a map to drawer layout.

Lord Goderick
  • 965
  • 2
  • 14
  • 32
  • That does make the map draw under the status bar, but then the problem is with the Compass UI and the My Location Button UI, which get pushed up under the Toolbar. Any idea on how to avoid that? – zyamys Jan 29 '17 at 06:33
  • 2
    You can use `googleMap.setPadding();` in your `onMapReady` to push down the map objects. – Lord Goderick Jan 29 '17 at 10:26
2

If you add your map in an Activity, then write these lines of code in your onCreate() method:

    window.apply {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                statusBarColor = Color.TRANSPARENT
        }
    }

If you add maps in a Fragment, then add these lines of code in onCreate() method:

    activity!!.window.apply {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                statusBarColor = Color.TRANSPARENT
        }
    }
Shariful Islam Mubin
  • 2,146
  • 14
  • 23
1

Setting fitsSystemWindows to false didn't worked for me

However, negative margin did the trick

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/app_bar_main"
    android:id="@+id/fragment_container"
    android:fitsSystemWindows="true"
    android:padding="0dp">
    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-25dp"
        map:uiCompass="false"
        android:fitsSystemWindows="true" />
</android.support.design.widget.CoordinatorLayout>
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86