Search code examples
androidgoogle-mapstooltiponclicklistenerlayout-inflater

Android: Button click event in custom tooltip Google Maps


I am trying to make a custom tooltip for my marker on my Google maps on the Android platform.

I tried some stuff and this is what I have for now.

Inflater class:

private class InfoHandler implements GoogleMap.InfoWindowAdapter, View.OnClickListener {

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        View v = layoutInflater.inflate(R.layout.tooltip_brewer, null);

        TextView tvLat = (TextView) v.findViewById(R.id.tv_title);
        TextView tvLng = (TextView) v.findViewById(R.id.tv_snippet);
        Button button = (Button) v.findViewById(R.id.btn_button);
        tvLat.setText("TestTooltip");
        tvLng.setText("TestTooltip 1");
        button.setOnClickListener(this);

        return v;
    }

    @Override
    public void onClick(View view) {
        Log.d(Constants.TAG,  "Test 01");
    }
}

Connection to the marker:

public void setInfoWindow(LayoutInflater inflater) {
        this.layoutInflater = inflater;
        map.setInfoWindowAdapter(new InfoHandler());
    }

Tooltip XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_snippet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="snippet" />

</LinearLayout>

<Button
    android:id="@+id/btn_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

Now everything works accept I cannot get the click event working on the button in the tooltip. Does somebody have a solution for that?


Solution

  • That is because it is not a live view so you cannot do what you want.

    Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.

    look at the documentation

    https://developers.google.com/maps/documentation/android/infowindows