You're all set!

To start developing, please head over to our developer documentation.

Activate the Google Maps Android API

To get you started we'll guide you through the Google Developers Console to do a few things first:

  1. Create or choose a project
  2. Activate the Google Maps Android API
  3. Create appropriate keys
Continue

Info Windows

An info window displays text or images in a popup window above the map. Info windows are always anchored to a marker. Their default behavior is to display when the marker is tapped.

Code samples

The ApiDemos repository on GitHub includes a sample that demonstrates all info window features:

Introduction

An info window allows you to display information to the user when they tap on a marker. Only one info window is displayed at a time. If a user clicks on a marker, the current info window will be closed and the new info window will be displayed. Note that if the user clicks on a marker that is currently showing an info window, that info window closes and re-opens.

An info window is drawn oriented against the device's screen, centered above its associated marker. The default info window contains the title in bold, with the (optional) snippet text below the title.

Add an info window

The simplest way to add an info window is to set the title() and snippet() methods of the corresponding marker. Setting these properties will cause an info window to appear whenever that marker is clicked.

static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE)
                          .title("Melbourne")
                          .snippet("Population: 4,137,400"));

Show/Hide an info window

Info windows are designed to respond to user touch events. If you prefer, you can show an info window programmatically by calling showInfoWindow() on the target marker. An info window can be hidden by calling hideInfoWindow().

static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE)
                          .title("Melbourne"));
melbourne.showInfoWindow();

Custom info windows

You are also able to customize the contents and design of info windows. To do this, you must create a concrete implementation of the InfoWindowAdapter interface and then call GoogleMap.setInfoWindowAdapter() with your implementation. The interface contains two methods for you to implement: getInfoWindow(Marker) and getInfoContents(Marker). The API will first call getInfoWindow(Marker) and if null is returned, it will then call getInfoContents(Marker). If this also returns null, then the default info window will be used.

The first of these (getInfoWindow()) allows you to provide a view that will be used for the entire info window. The second of these (getInfoContents()) allows you to just customize the contents of the window but still keep the default info window frame and background.

The images below show a default info window, an info window with customized contents, and an info window with customized frame and background.

Info Window Comparison

Info window events

The MarkerDemoActivity sample includes example code for registering and handling info window events.

You can use an OnInfoWindowClickListener to listen to click events on an info window. To set this listener on the map, call GoogleMap.setOnInfoWindowClickListener(OnInfoWindowClickListener). When a user clicks on an info window, onInfoWindowClick(Marker) is called and the info window is highlighted in the default highlight color (gray).

public class MarkerDemoActivity extends AppCompatActivity implements
        OnInfoWindowClickListener,
        OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;
        // Add markers to the map and do other map setup.
        ...
        // Set a listener for info window events.
        mMap.setOnInfoWindowClickListener(this);
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        Toast.makeText(this, "Info window clicked",
                Toast.LENGTH_SHORT).show();
    }
}

Similarly, you can listen for long click events with an OnInfoWindowLongClickListener, which you can set by calling GoogleMap.setOnInfoWindowLongClickListener(OnInfoWindowLongClickListener). This listener behaves similarly to the click listener and will be notified on long click events with an onInfoWindowLongClick(Marker) callback.

To be notified when the info window closes, use an OnInfoWindowCloseListener, which you can set by calling GoogleMap.setOnInfoWindowCloseListener(OnInfoWindowCloseListener). You will receive an onInfoWindowClose(Marker) callback.

Note about refreshing an info window: The onInfoWindowClose() event fires if the user refreshes an info window by tapping a marker that already has an open info window. But if you programmatically call Marker.showInfoWindow() on an open info window, the onInfoWindowClose() event does not fire. The latter behavior is based on the assumption that you are aware that the info window will close and re-open.

As mentioned in the previous section on info windows, an info window is not a live View. Instead, the view is rendered as an image on the map. As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view. You are advised not to place interactive components — such as buttons, checkboxes, or text inputs — within your custom info window.

Send feedback about...

Google Maps Android API
Google Maps Android API
Need help? Visit our support page.