Search code examples
androidgoogle-mapsandroid-fragmentsfloating-action-button

Floating action button on top of map fragment


I want to Place floating action button on top of map fragment. My map fragment is completely filled the entire screen. When I place a floating action button it is not coming on top of map fragment Please refer the image attached:

map fragment with search bar

Please find layout code here

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:orientation="vertical"
        android:weightSum="1">


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_small"
            >

            <fragment
                android:id="@+id/autocomplete_fragment"
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
            <!--<ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/bell32"
                android:visibility="invisible"
                />
            <android.support.v7.widget.SwitchCompat
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/alarm_on_switch"
                android:layout_gravity="right|top"
                android:layout_marginRight="@dimen/margin_small"
                android:drawableLeft="@drawable/bell32"
                />-->

        </android.support.v7.widget.CardView>
        <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"
            tools:context="reminder.locrem.com.locationreminder.MapsActivity"
            android:layout_height="match_parent" />

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|right"
            android:layout_margin="16dp"
            android:src="@drawable/done"
            app:layout_anchorGravity="bottom|right|end"
            app:elevation="4dp" />

    </LinearLayout>

</ScrollView>

Solution

  • You can use FrameLayout to acieve what you want.

        <?xml version="1.0" encoding="utf-8"?>
    <ScrollView 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="1">
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin_small">
                <fragment
                    android:id="@+id/autocomplete_fragment"
                    android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
            </android.support.v7.widget.CardView>
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <fragment
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context="reminder.locrem.com.locationreminder.MapsActivity" />
    
                <android.support.design.widget.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top|right"
                    android:layout_margin="16dp"
                    android:src="@drawable/done"
                    app:elevation="4dp"
                    app:layout_anchorGravity="bottom|right|end" />
            </FrameLayout>
    
        </LinearLayout>
    </ScrollView>