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();
Try this in any activity:
snackbar(findViewById(android.R.id.content),"your text")
As Shahab Rauf points out, getting the view via getDecorView() may put the snackbar behind the navigation bar in the bottom. I use the following code: (Use and extend to your delight)
public class BaseActivity extends AppCompatActivity { private View getRootView() { final ViewGroup contentViewGroup = (ViewGroup) findViewById(android.R.id.content); View rootView = null; if(contentViewGroup != null) rootView = contentViewGroup.getChildAt(0); if(rootView == null) rootView = getWindow().getDecorView().getRootView(); return rootView; } protected void showSnackBarWithOK(@StringRes int res) { final View rootView = getRootView(); if(rootView != null) { final Snackbar snackbar = Snackbar.make(getRootView(), res, Snackbar.LENGTH_INDEFINITE); snackbar.setAction(R.string.ok, new View.OnClickListener() { @Override public void onClick(View v) { snackbar.dismiss(); } }); snackbar.show(); } } protected void showSnackBar(@StringRes int res) { final View rootView = getRootView(); if(rootView != null) Snackbar.make(rootView, res, Snackbar.LENGTH_LONG).show(); }}