Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the position of compass in mapview [duplicate]

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();
    }
like image 865
Libin Antony Avatar asked Sep 14 '25 02:09

Libin Antony


1 Answers

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

like image 116
Lalit Fauzdar Avatar answered Sep 16 '25 16:09

Lalit Fauzdar