9

I can customize the content of the infoWindow with a simple block of code:

 private GoogleMap mMap;
 mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

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

        @Override
        public View getInfoContents(Marker arg0) {

            View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
            return v;

        }
    });

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#55000000" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="HELLO"
        android:textColor="#FF0000"/>

</LinearLayout>

But this changes only the content and not the infoWindow itself. It still stays white with a small shadow on the bottom and now a red HELLO text is visible with black bg. I want to change this default infoWindow to a transparent black rectangle. How can I do this?

enter image description here

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
erdomester
  • 11,789
  • 32
  • 132
  • 234

4 Answers4

35

you have to write your code in getInfoWindow method. so you can customize info window. e.g.

@Override
public View getInfoWindow(Marker marker) {

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

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

    TextView address = (TextView) v.findViewById(R.id.distance);
    address.setText(marker.getSnippet());

    return v;
}

@Override
public View getInfoContents(Marker arg0) {
    // TODO Auto-generated method stub
    return null;
}
Ankit Sharma
  • 1,261
  • 2
  • 13
  • 15
  • 2
    This is the correct answer, must use getInfoWindow not not use the default white box. – jb15613 Dec 16 '13 at 19:02
  • You need to setInfoWindowAdapter() right after map.addMarker() method for this to take effect. Do not call setInfoWindowAdapter() in onMarkerClick(). – Melbourne Lopes Jan 23 '14 at 09:45
  • i would recommend inflating the view before calling the function, and only change relevant info when getInfoWindow() is called – Dan Levin May 07 '14 at 10:37
10

enter image description here

use this way...

myMap.setInfoWindowAdapter(new InfoWindowAdapter() {

                @Override
                public View getInfoWindow(Marker arg0) {

                    ContextThemeWrapper cw = new ContextThemeWrapper(
                            getApplicationContext(), R.style.Transparent);
                    // AlertDialog.Builder b = new AlertDialog.Builder(cw);
                    LayoutInflater inflater = (LayoutInflater) cw
                            .getSystemService(LAYOUT_INFLATER_SERVICE);
                    View layout = inflater.inflate(R.layout.custom_infowindow,
                            null);
                    return layout;
                }

                @Override
                public View getInfoContents(Marker arg0) {

                    return null;

                }
            });

R.style.Transparent add this in your style.xml

<style name="Transparent" parent="android:Theme.Light">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

    <color name="transparent">#00000000</color>

Edited:

custom_infowindow.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:text="HELLO" />

</LinearLayout>
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • This doesn't change anyhing. I get the same output as before. However, I see it is working for you. I removed all the styles but in vain... – erdomester May 01 '13 at 20:54
  • @erdomester: i have added custom_infowindow.xml in my answer.. just use all my code insted of yours... and tell me its working or not.. – Dhaval Parmar May 02 '13 at 04:45
  • I still get the default layout with the red HELLO: http://www.2shared.com/photo/lBOzOHMz/hello.html I am testing on 2.3.5 – erdomester May 04 '13 at 18:14
  • @Dhawal Sodha Parmar Your answer is great Thank you so much.But can you please tell me diffrence between these two methods:1. public View getInfoWindow(Marker arg0) {} and 2. public View getInfoContents(Marker arg0) {} – AndiM Jun 17 '14 at 11:19
  • @AndiM: check these: http://stackoverflow.com/questions/17253019/difference-between-getinfowindow-and-getinfocontents-in-googlemaps-v2-for-an – Dhaval Parmar Jun 17 '14 at 11:24
  • Thank you @Dhawal Sodha Parmar – AndiM Jun 17 '14 at 12:20
  • @Dhawal Sodha Parmar I have One more question.Can you please tell me how to respond to Click listener of view inside CustomInfoWindow instead of onInfoWindowCLicklistener? – AndiM Jun 17 '14 at 12:26
1

Try this...I have change little bit of your code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 >

   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="#80000000" 
    >

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:text="HELLO"
     android:textColor="#FF0000"
     android:padding="10dp"/>

   </LinearLayout> 

</LinearLayout>

Also change this...

 mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        public View getInfoWindow(Marker arg0) {
            View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
            return v;
        }

        public View getInfoContents(Marker arg0) {

            //View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);

            return null;

        }
    });

I hope it will work...:)

TheFlash
  • 5,997
  • 4
  • 41
  • 46
1

You need to setInfoWindowAdapter() right after map.addMarker() method for this to take effect. Do not call setInfoWindowAdapter() in onMarkerClick().

Melbourne Lopes
  • 4,817
  • 2
  • 34
  • 36