Set custom InfoWindow width in Google Maps api v2

I created map markers. When I click on them, custom info windows always have a screen width.

I tried setting layout_width="200dp" , but that didn't help. I also tried to install

 view.setLayoutParams(new LayoutParams(GetDipsFromPixel(200), GetDipsFromPixel(300))); 

I can set the width of the text views by setting snippetUi.setWidth(GetDipsFromPixel(200)); but the layout still remains widescreen.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="#FFFFFF" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:padding="10dp" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#FFFFFF" android:gravity="center_vertical"> <ImageView android:id="@+id/worldmap_infowindow_profileimg" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/noimage" /> <TextView android:id="@+id/worldmap_infowindow_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To do No.2." android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" android:paddingLeft="5dp"/> </LinearLayout> <View android:id="@+id/separator" android:background="#ababab" android:layout_width = "fill_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_height="1dp"/> <TextView android:id="@+id/worldmap_infowindow_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="See the Giza Pyramids" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/worldmap_infowindow_details" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="It was fascinating! It a shame that it was raining, though." android:textColor="#000000" android:textSize="14sp" android:textStyle="normal" /> </LinearLayout> 

Grade:

  private class CustomInfoWindowAdapter implements InfoWindowAdapter { private View view; public CustomInfoWindowAdapter() { view = getLayoutInflater().inflate(R.layout.custom_infowindow, null); } @Override public View getInfoContents(Marker marker) { if (WorldMap.this.myMarker != null && WorldMap.this.myMarker.isInfoWindowShown()) { WorldMap.this.myMarker.hideInfoWindow(); WorldMap.this.myMarker.showInfoWindow(); } return null; } @Override public View getInfoWindow(final Marker marker) { WorldMap.this.myMarker = marker; final String title = marker.getTitle(); final TextView titleUi = ((TextView) view.findViewById(R.id.worldmap_infowindow_name)); if (title != null) { titleUi.setText(title); } else { titleUi.setText(""); } final String snippet = marker.getSnippet(); final TextView snippetUi = ((TextView) view.findViewById(R.id.worldmap_infowindow_details)); if (snippet != null) { snippetUi.setText(snippet); } else { snippetUi.setText(""); } return view; } } 

enter image description here

+7
android google-maps-api-2
source share
5 answers

If you want to show a custom window with a fixed size (Height or Width), you must make drawable and set it as an XML background.

I sent my answer HERE using the solution. You can specify how it works.

+5
source share

Basically, you just need to have another layout level below root, for example:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:background="@android:color/transparent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="250dp" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="horizontal"> ... 
+41
source share

Just create a parent layout with the "wrap_content" parameter and a child layout with the required size:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <!-- This layout defines the desired size --> <LinearLayout android:layout_width="240dp" android:layout_height="320dp" android:orientation="vertical"> <!-- Your views here --> </LinearLayout> </LinearLayout> 
+3
source share

In my case, I set the fixed width of one of the children of the layout, and it worked. I don't know why this did not work when adjusting the width of the root view. Here is an example:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" android:padding="3dp"> <ImageView android:id="@+id/info_window_image" android:layout_width="200dp" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/facebook"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="1" android:maxLines="1" android:singleLine="true" android:text="Free for All Soccer Game" android:textColor="@color/blue" android:textSize="@dimen/medium" /> </LinearLayout 
<P β†’
+1
source share
 @Override public View getInfoWindow(Marker arg0) { return null; } @Override public View getInfoContents(Marker arg0) { View v = getActivity().getLayoutInflater().inflate(R.layout.custom_info_window, null); v.setLayoutParams(new LinearLayout.LayoutParams(500,300)); } 
0
source share

All Articles