MapFragment gets a dark overlay when used in DialogActivity
Asked Answered
C

5

23

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:

Dialog

When I change the theme to @android:style/Theme.DeviceDefault.Light.NoActionBar, the map is shown as it should:

Normal

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?

Calcareous answered 29/1, 2013 at 17:50 Comment(1)
Maybe it helps you: #14127094.Nemesis
S
18

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

Sportive answered 24/4, 2013 at 9:19 Comment(0)
S
11

For DialogFragment:

getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Seventeen answered 1/8, 2015 at 10:7 Comment(2)
While the code may answer the question, it would be better to explain how it works and why to use it.Libbielibbna
This solution is useful for transparent activities that already have a dimmed background (which does not interfere with Google Maps), especially as the default DialogFragment dimmed background is useless, and makes it too darkHardboiled
P
1

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...

Prescription answered 10/9, 2015 at 6:23 Comment(2)
Thank you for sharing, I think this should be added: getWindow().setAttributes(params); Decrement
BTW, I use AMap from China, I guess GoogleMap already fixed this issue long time ago, am I right?Decrement
C
0

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

Candless answered 12/5, 2016 at 21:40 Comment(0)
B
0

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>
Barbarian answered 24/7, 2017 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.