-
-
Save himangipatel/7d4232a7e70f815f8ff446211cc6ac6f to your computer and use it in GitHub Desktop.
Custom InfoWindow Layout for Google Map in Android
This file contains 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 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 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment