CodingSingh
CodingSingh

Reputation: 113

Move floating action button above directions popup in Google Maps API fragment?

I am using the Google Maps API in Android Studio (through the default Google Maps Activity). I enabled setMyLocationEnabled(true) but set setMyLocationButtonEnabled false since I wanted to add a custom Floating Action Button, which looks better to me, for moving to the current location. When a marker is clicked, the API automatically generates direction buttons at the bottom right of the maps fragment to that marker however the FAB that I've implemented covers those buttons up.

FAB covers API directions

After looking at this answer I wrapped the maps fragment and floating action button in a CoordinatorLayout, hoping that it would move the FAB up when a marker is clicked, as it does with snackbars, but it doesn't. Is there a way to activate that motion manually or at least simulate it with touch events or something similar? I tried to look for ways to move the FAB at runtime (such as this) but I was unsuccessful in finding a fitting solution for my situation.

Here is the xml for the layout for the activity, let me know if more information is needed. Thanks in advance!

<android.support.design.widget.CoordinatorLayout 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">

    <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="(hidden for privacy)"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/find_my_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_my_location_gray"
        app:backgroundTint="@color/white"/>
</android.support.design.widget.CoordinatorLayout>

Upvotes: 2

Views: 2238

Answers (1)

gizembrh
gizembrh

Reputation: 140

I suppose giving android:layout_marginBottom to your FAB from xml does not work or you don't prefer this solution, so:

You may either hide direction buttons at the bottom with setMapToolbarEnabled() if you don't need them: googleMap.getUiSettings().setMapToolbarEnabled(false);

or listen for marker clicks with GoogleMap.OnMarkerClickListener:

googleMap.setOnMarkerClickListener(this);

and then do whatever you want in onMarkerClick():

@Override
public boolean onMarkerClick(Marker marker) {
        //give some margin programmatically
        //return false to preserve the default behaviour, true o/w 
        return true;
}

Upvotes: 1

Related Questions