I'm trying to change the zoom & my location controls positions in Android Google maps.
I want to set zoom control position at the right bottom and set my location position at the left bottom of MapFragment
(I'm using the com.google.android.gms.maps.SupportMapFragment
).
Here is my map fragment layout:
<fragment
android:id="@+id/map"
android:layout_below="@+id/toolbar"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The written codes for changing the controls position (getMapAsync
callback):
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if (map != null) {
// Map is ready
map.getUiSettings().setZoomControlsEnabled(true);
View mapView = mapFragment.getView();
// Zoom control
@SuppressLint("ResourceType") View zoomButton = mapView.findViewById(0x1);
if (zoomButton != null && zoomButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams zp = (RelativeLayout.LayoutParams) zoomButton.getLayoutParams();
zp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
zp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
zp.setMargins(0, 0, 30, 30);
}
// My location control
@SuppressLint("ResourceType") View locationButton = mapView.findViewById(0x2);
if (locationButton != null && locationButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on left bottom
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(30, 0, 0, 30);
}
}
But it doesn't work and I'd like to know what's wrong with my code. Is there a better solution for doing that?
[Updated - 1]
My application language is Persian (FA) so I've added android:supportsRtl="true"
in the AndroidManifest.xml
file in order to support RTL language.
Problem: The zoom control is aligned at the bottom left which I would reposition it at bottom right corner.
You can find Location button on map by getting parent view first and then "My Location" button as below :
// Get the "My Location" button view
View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2);
// get button params to place the button
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// place on right bottom corner
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
If want to resize button, you can get parameters like below :
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 100);
//.... do your editing
// To set params to button
locationButton.setLayoutParams(lp);