21

This is my custom layout for my info window:

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_infowindow" >
    <LinearLayout
            android:id="@+id/text_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        <TextView 
            style="@style/TexTitle"
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView 
            style="@style/TextDistance"
            android:id="@+id/distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

And this is my custom adapter:

public class MapInfoWindowAdapter implements InfoWindowAdapter{

    private LayoutInflater inflater;
    private Context context;

    public MapInfoWindowAdapter(Context context){
        inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        this.context = context;
    }

    @Override
    public View getInfoContents(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 getInfoWindow(Marker arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

And this is the result:

enter image description here

However I want the custom drawable as the only background for my info window, How to achieve this?

IsaacCisneros
  • 1,953
  • 2
  • 20
  • 30

3 Answers3

59

Replace codes in getInfoContents with getInfoWindow. The difference between them is getInfoContents wraps your View in ViewGroup with default background.

@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;
}
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • 2
    I really appreciate your clarifying answer, however I was getting a NullPointerException using RelativeLayout as a root in my custom layout so I'm using a LinearLayout instead. – IsaacCisneros May 13 '13 at 10:43
  • 1
    @IsaacCisneros That should probably be your original question then. This is a known issue. See this: [gmaps-api-issues](https://code.google.com/p/gmaps-api-issues/issues/detail?id=4748). – MaciejGórski May 13 '13 at 10:46
  • @MaciejGórski I used the same code. It is working fine. I got trouble when click once again on the same window i hided the window. But in background the default info window shown remain. Have you removed that? – Parthi Jul 27 '15 at 07:30
  • watsted 3 hours until I found your answer. Thank you – Khaled Alhayek Nov 20 '18 at 15:54
0

try this..

custom_infowindow.xml

<?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" 
android:orientation="vertical">

<ImageView
    android:src="@drawable/ic_launcher"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:padding="10dp"/>

</LinearLayout> 

</LinearLayout>


googleMap.setInfoWindowAdapter(new InfoWindowAdapter() 
{

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

        public View getInfoContents(Marker arg0) 
        {
           return null;
        }
    });
TheFlash
  • 5,997
  • 4
  • 41
  • 46
0

in addition to the accepted answer i want to mention that if you still want the info window bubble you can use this bubble layout library

then you can set the bubble layout as background of your route view in the custom layout you are inflating.

A.sobhdel
  • 234
  • 4
  • 11