Making a Snackbar Without a View?

I see some options… Not sure which one can fix your issue.

Simpliest

SupportMapFragment extends class android.support.v4.app.Fragment. This way, it has a method getView()

Snackbar.make(mapFragment.getView(), "Click the pin for more options", Snackbar.LENGTH_LONG).show();

Find Root View

From this answer, there’s a way to get the root view via:

getWindow().getDecorView().getRootView()

So, maybe, you can do:

Snackbar.make(getWindow().getDecorView().getRootView(), "Click the pin for more options", Snackbar.LENGTH_LONG).show();

NOTE: This approach has the side effect mentioned in the comments below:

The message will be shown behind the bottom navigation bar if the following method will be used to get view getWindow().getDecorView().getRootView()

Add a dummy LinearLayout to get the View

Honestly, I’m not sure if this solution is possible. I’m not sure if you can add a LinearLayout above the Maps fragment… I think it is OK but since I never work with Maps API before, I’m not sure.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dummy_layout_for_snackbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment 
        xmlns:map="http://schemas.android.com/apk/res-auto"
        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="ca.davesautoservice.davesautoservice.MapsActivity" />
</LinearLayout>

and then:

Snackbar.make(findViewById(R.id.dummy_layout_for_snackbar), "Click the pin for more options", Snackbar.LENGTH_LONG).show();

Leave a Comment