Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google maps transparency

I'm working on a mapping app. And I want to show some views under the MapView. I'm using the method:

    mapView.setAlpha(0.5f);

Nothing happens to the mapView. When I try to apply this method to my buttons it works fine, even applying it to the parent view of the mapView makes every child view transparent except for the mapView.

How can we make the mapView transparent? I have looked into other questions here and I cant seem to find a solution.

Is there some special method in the mapView.getMap() that we can use to make it transparent?

like image 379
Marlon Avatar asked Sep 10 '15 17:09

Marlon


1 Answers

You can set the transparence for the map using View.setAlpha() (documentation). Here is a working example:

activity_maps.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="THIS TEXT IS VISIBLE"
        android:textSize="24sp"
        android:gravity="center"/>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Marker m = googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        m.showInfoWindow();

        View v = getSupportFragmentManager().findFragmentById(R.id.map).getView();
        v.setAlpha(0.5f); // Change this value to set the desired alpha
    }
}

The result looks like this (as you see the text is visible because of the map's transparency):

enter image description here

like image 127
antonio Avatar answered Oct 14 '22 17:10

antonio