3

enter image description here

I need to change the position of compass from left to right in app.It always shows in left.Is it possible to change position??

  // change compass position
    try {
        assert mapFragment.getView() != null;
        final ViewGroup parent = (ViewGroup) mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
        parent.post(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0, n = parent.getChildCount(); i < n; i++) {
                        View view = parent.getChildAt(i);
                        RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        // position on right bottom
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                        rlp.rightMargin = rlp.leftMargin;
                        rlp.topMargin = 25;
                        view.requestLayout();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
Libin Antony
  • 61
  • 1
  • 4

2 Answers2

5

Here's the needed solution to align the Compass button on Top Right corner:

View compassButton = mMapView.findViewWithTag("GoogleMapCompass");//to access the compass button
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) compassButton.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_END);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_START,0);
rlp.topMargin = 50;

This will change it's RelativeLayout params to align to the Top Right corner.

Result:

enter image description here

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
0

You may use GoogleMap.setPadding() method: for your compass position

https://developers.google.com/maps/documentation/android/map#map_padding

diksha
  • 109
  • 8