Search code examples
androidgoogle-mapsgoogle-maps-api-3google-maps-markersinfowindow

Unable to set Custom info windows of Google Maps


I'm trying to set Custom info windows using this tutorial.

Here's my code:

public class MyActivity extends AppCompatActivity implements OnMapReadyCallback, ClickListenerChatFirebase, GoogleMap.InfoWindowAdapter {

public void showMap() {
        venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
        venueMarker.showInfoWindow();
        getInfoWindow(venueMarker);
    }

public AcceptedRequest(LayoutInflater inflater){
        this.inflater = inflater;
    }

    @Override
    public View getInfoWindow(Marker marker) {

        // Getting view from the layout file
//        inflater = getLayoutInflater();
        View v = inflater.inflate(R.layout.venue_infowindow_background, null);

        TextView title = (TextView) v.findViewById(R.id.venue_txt);
        title.setText(marker.getTitle());

        return v;
    }

    @Override
    public View getInfoContents(Marker arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

}

Here's venue_infowindow_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@color/colorPrimary"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <TextView
        android:id="@+id/venue_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="venue"
        android:textSize="14sp"
        android:textStyle="bold"
        android:textColor="@android:color/white"/>

</LinearLayout>

The problem is that no change is happening and the info window is showing the default text-color and background-color.

Please help with this issue.


Solution

  • You can't just directly call the getInfoWindow() method override.

    You need to set the InfoWindowAdapter for the Google Map to your Activity, which implements the GoogleMap.InfoWindowAdapter interface.

    public void showMap() {
        venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
        venueMarker.showInfoWindow();
    
        //This won't work:
        //getInfoWindow(venueMarker); 
    
        //Do this instead:
        mMap.setInfoWindowAdapter(this);
    }
    

    In order to use a different layout for this Marker, you could inflate a different custom layout for all other Markers:

    @Override
    public View getInfoWindow(Marker marker) {
        View v = null;
        if (marker.equals(venueMarker)) {
            v = inflater.inflate(R.layout.venue_infowindow_background, null);
        } else {
            v = inflater.inflate(R.layout.some_other_layout, null);
        }
    
        TextView title = (TextView) v.findViewById(R.id.venue_txt);
        title.setText(marker.getTitle());
    
        return v;
    }
    

    In order to use the "speech bubble" with the default Markers, and not with the one custom Marker, you can use getInfoWindow() with the custom Marker, and getInfoContents() for the default Markers. (More info here). Something like this might work.:

    @Override
    public View getInfoWindow(Marker marker) {
        if (!marker.equals(venueMarker)) {
            return null;
        }
        View v = inflater.inflate(R.layout.venue_infowindow_background, null);
        TextView title = (TextView) v.findViewById(R.id.venue_txt);
        title.setText(marker.getTitle());
    
        return v;
    }
    
    @Override
    public View getInfoContents(Marker arg0) {
        if (marker.equals(venueMarker)) {
            return null;
        }
        View v = inflater.inflate(R.layout.some_other_layout, null);
        TextView title = (TextView) v.findViewById(R.id.venue_txt);
        title.setText(marker.getTitle());
    
        return v;
    }