1

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.

Community
  • 1
  • 1
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

1 Answers1

1

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;
}
Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • this did the job but changed the background and textcolor of all the markers... how can I change it for 1 particular marker? – Hammad Nasir Jan 12 '17 at 20:11
  • You could implement your `getInfoWindow()` override such that it inflates one layout for the one Marker, and a different layout for all other Markers. – Daniel Nugent Jan 12 '17 at 20:31
  • but how? please elaborate a bit. – Hammad Nasir Jan 13 '17 at 01:22
  • I updated the answer, take a look at the second code block. If you want to make the other Markers look like the default, just make `some_other_layout.xml` use a white background and black text. – Daniel Nugent Jan 13 '17 at 01:37
  • Great! That worked, but how can I make the same info window as the default one? Like with that down-pointing part just above the marker? – Hammad Nasir Jan 13 '17 at 02:01
  • 1
    I just updated the answer with something that might work. I didn't test it though! – Daniel Nugent Jan 13 '17 at 04:02
  • it worked, but I'm getting a weird padding like shown here: http://stackoverflow.com/questions/18282718/change-the-margin-color-of-infowindow-view-of-marker-in-google-maps-api-v2 How to remove this? Any idea? – Hammad Nasir Jan 13 '17 at 04:16
  • 1
    I just updated the answer again, originally I had the logic wrong regarding when to use the speech bubble. Take a look at the latest code. – Daniel Nugent Jan 13 '17 at 04:18
  • yeah, but the weird padding is still there... that is the problem now :| – Hammad Nasir Jan 13 '17 at 04:19
  • any solution regarding weird padding? – Hammad Nasir Jan 13 '17 at 09:09
  • @HammadNasir There is no easy solution. Basically, if you're using the default white "speech bubble", you will need to use a light background. For a dark background speech bubble, you will need to do a custom one yourself. Basically, use `getInfoWindow()`, and in the layout xml, define a custom drawable as the background that has the shape of a speech bubble (you could use png or xml drawables). You might need to use Photoshop to make custom png assets if you can't find anything pre-made. – Daniel Nugent Jan 13 '17 at 17:53