Last active
February 27, 2024 12:40
-
-
Save ccjeng/ff8ca25e0e92302639dadbe4a8533279 to your computer and use it in GitHub Desktop.
Custom InfoWindow Layout for Google Map in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
thank very much bro. You are hero tonight
Can we use a bottom sheet for this
thnkx u made my day
Lovely
Awesome
if getInfoWindow return view contains RecyclerView, how can i solve the MotionEvent .
Now the recyclerView not response events,
thank you very much
thnx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks very much.
It helped me.