I know it's been answered and accepted but these answers didn't work for me. Perhaps there have been changes in the RelativeLayout.LayoutParams or even in GoogleMaps since it was answered.
In my attempts with the existing answers I realized that there may be more LayoutParam rules for the compass button that are overriding my attempts to move the compass button where I wanted it. I experimented by removing several params like this:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
locationButton.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
And then adding the new rules as they did in several of the answers. But it still did not work as expected. More often than not, the button stayed somewhere on the left side.
I decided to just create new LayoutParams and replace the existing ones and ... it worked perfectly!
I originally used the view tag for GoogleMapCompass
, but the question pertained to the GoogleMapMyLocationButton
. So I commented out the GoogleMapCompass
line and put in the GoogleMapMyLocationButton
line.
Here is the code for MapFragment.java:
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class MapFragment extends Fragment implements OnMapReadyCallback {
private static final String TAG = MapFragment.class.getSimpleName();
private SupportMapFragment mapFragment = null;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
Log.d(TAG, "onCreateView()");
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.setRetainInstance(true);
mapFragment.getMapAsync(this);
return view.findViewById(R.id.map);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady()");
View mapView = mapFragment.getView();
moveCompassButton(mapView);
}
/**
* Move the compass button to the right side, centered vertically.
*/
public void moveCompassButton(View mapView) {
try {
assert mapView != null; // skip this if the mapView has not been set yet
Log.d(TAG, "moveCompassButton()");
// View view = mapView.findViewWithTag("GoogleMapCompass");
View view = mapView.findViewWithTag("GoogleMapMyLocationButton");
// move the compass button to the right side, centered
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.setMarginEnd(18);
view.setLayoutParams(layoutParams);
} catch (Exception ex) {
Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage());
ex.printStackTrace();
}
}
}