Skip to content

Instantly share code, notes, and snippets.

@ccjeng
Last active February 27, 2024 12:40
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ccjeng/ff8ca25e0e92302639dadbe4a8533279 to your computer and use it in GitHub Desktop.
Save ccjeng/ff8ca25e0e92302639dadbe4a8533279 to your computer and use it in GitHub Desktop.
Custom InfoWindow Layout for Google Map in Android
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Activity context;
public CustomInfoWindowAdapter(Activity context){
this.context = context;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View view = context.getLayoutInflater().inflate(R.layout.custominfowindow, null);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
TextView tvSubTitle = (TextView) view.findViewById(R.id.tv_subtitle);
tvTitle.setText(marker.getTitle());
tvSubTitle.setText(marker.getSnippet());
return view;
}
}
<!--Layout for Custom InfoWindow-->
<?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">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapFragment mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment));
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setZoomControlsEnabled(true);
String title = "This is Title";
String subTitle = "This is \nSubtitle";
//Marker
MarkerOptions markerOpt = new MarkerOptions();
markerOpt.position(new LatLng(Double.valueOf(strToLat), Double.valueOf(strToLng)))
.title(title)
.snippet(subTitle)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
//Set Custom InfoWindow Adapter
CustomInfoWindowAdapter adapter = new CustomInfoWindowAdapter(MainActivity.this);
map.setInfoWindowAdapter(adapter);
map.addMarker(markerOpt).showInfoWindow();
}
}
@asantibanez
Copy link

Works like a charm. Thanks!

@shihabmi7
Copy link

shihabmi7 commented Jan 24, 2018

what if, I want to add a marker on runtime & for multiple markers?

@sohel2178
Copy link

Thanks Man

@truclc
Copy link

truclc commented May 7, 2018

Thanks bro.
This save my time.

@DevMobile2
Copy link

how can i write onClick events for view in info window.The whole info window is clicking rather than individual views in it.

@ByeongsuPark
Copy link

Thanks very much.
It helped me.

@sogatanco
Copy link

thank very much bro. You are hero tonight

@ytheekshana
Copy link

Can we use a bottom sheet for this

@asadghaffar
Copy link

thnkx u made my day

@naing-pyae-hlyan
Copy link

Lovely

@holoc285
Copy link

Awesome

@zhaiysgithub
Copy link

if getInfoWindow return view contains RecyclerView, how can i solve the MotionEvent .
Now the recyclerView not response events,
thank you very much

@Ahmed4240
Copy link

thnx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment