Search code examples
androidandroid-mapviewandroid-coordinatorlayoutandroid-collapsingtoolbarlayoutandroid-statusbar

MapView under statusbar with CollapsingToolbarLayout


I have the following XML code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/abl_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/ctl_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                xmlns:map="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="280dp"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.5"
                map:cameraZoom="15"
                map:liteMode="true"
                map:mapType="normal"/>


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:elevation="@dimen/spacing_tiny"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_pin"
        style="@style/FabStyle"
        app:layout_anchor="@id/abl_header"
        app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>

I would like to have the MapView (fragment here) draw below the status bar as on the picture below: Expected result

But instead I have the following result: Not working result

But when scrolling, the MapView is correctly drawn below the status bar as shown on the picture below: MapView drawn below when scrolling

And because, this is working when using an ImageView instead of the fragment, I am guessing this is because of a wrong measurement when the fragment is being build.

I would like to know if there is other solution than creating the MapView and creating the Bitmap from the code before injecting the bitmap in an ImageView.


Solution

  • I figure out how to solve this.

    Instead of creating the MapView fragment from the layout file itself. I create the SupportMapFragment and add it to the view using classic Fragment transaction.

    In that way, I have a correct rendering.