I'm trying to show a MapFragment
of the Android Maps v2 API
in an Activity
with the @android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar
theme. This works fine. However, the map gets a dark overlay:
When I change the theme to @android:style/Theme.DeviceDefault.Light.NoActionBar
, the map is shown as it should:
This is my xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/mapfragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.google.android.gms.maps.MapFragment" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HELLO"
android:textSize="@dimen/textsize_large" />
</LinearLayout>
What is happening here?
I got the same problem. After some research, I found two hacks :
- Add this param for your theme in the styles.xml :
<item name="android:backgroundDimEnabled">false</item>
Bad part : it removes the black overlays behind the activity.
- Set the z order on the top for the map object :
GoogleMapOptions googleMapsOptions = new GoogleMapOptions();
googleMapsOptions.zOrderOnTop( true );
MapFragment mapFragment = MapFragment.newInstance(googleMapsOptions);
Bad part : The map is above the Zoom control and MyLocation button.
Personnaly, I choose the first solution.
Hope this help !
Link to the source
For DialogFragment
:
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
To add on to Audrel's solution, one can dynamically un-dim the dialog's window whenever the map is shown and dim it back when the map is gone (e.g. when your dialog has multiple pages, one of them is a map):
private float mDimAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams params = getWindow().getAttributes();
mDimAmount = params.dimAmount; // remember original dim amount
...
}
private void undim() {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.dimAmount = 0.0f;
getWindow().setAttributes(params);
}
private void dim() {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.dimAmount = mDimAmount;
getWindow().setAttributes(params);
}
Still just another workaround rather than tackling the root cause...
Depending on what your purpose is for the map in the dialog, if you don't need any of the map controls, you can use liteMode
in which just an image of the map is displayed with whatever location you load. It doesn't have this z-layer overlay problem.
You can add this to the MapView
layout xml:
map:liteMode="true"
This worked for me, because I didn't want my map to be interactive anyway.
You can read the Google docs here: https://developers.google.com/maps/documentation/android-api/lite
Simply add this line to your style !
<item name="android:backgroundDimEnabled">false</item>
This is my style :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/dark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="windowActionBar">false</item>
</style>