5

I have a fragment that contains a google map:

<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraZoom="13"
    map:mapType="normal"
    map:uiZoomControls="false"
    map:uiRotateGestures="true"
    map:uiScrollGestures="true"
    map:uiZoomGestures="true"
map:uiTiltGestures="false" />

the map takes up the entire screen and has mylocation enabled:

 map.setMyLocationEnabled(true);

The following code should align the mylocation button to the top-left of the screen:

 // Get the mylocation button view
                    View locationButton = ((View) mapview.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));

                RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();

                rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

however - this only aligns the button to the top center of the screen. However, if I add this:

if (Build.VERSION.SDK_INT >= 17) {
                               rlp.addRule(RelativeLayout.ALIGN_PARENT_START);
                        }

then it aligns to the left properly. The problem is that ALIGN_PARENT_START can't be used programmatically before api 17 and I need to support from a lower api level. Why isn't align_parent_left working on it's own to align the button to the left?

Jon
  • 7,941
  • 9
  • 53
  • 105
  • 1
    try making your mapview's parent view to RelativeLayout or FrameLayout. – MetaSnarf Jul 23 '15 at 15:53
  • 1
    check this: http://stackoverflow.com/questions/28688470/android-google-maps-api-change-position-of-maps-toolbar/31337222#31337222 – MetaSnarf Jul 23 '15 at 15:54
  • @MetaSnarf - it is a relativelayout - otherwise it wouldn't work at all (even with align_parent_start). Regarding the link that you gave -> this is the method that I'm already using to move the mylocation button, however it doesn't explain why align_parent_left centers the button while align_parent_start aligns it to the left of the screen – Jon Jul 23 '15 at 16:02
  • 1
    how about a frame layout? – MetaSnarf Jul 23 '15 at 16:13
  • @MetaSnarf - tried this, including replacing the rootlayout of the xml with a framelayout - but no matter how I do it I always receive an error that framelayout cant be cast to relativelayout. It looks like it's defined internally as a relativelayout in some way – Jon Jul 23 '15 at 16:46
  • 1
    i have posted an answer. That is my implementation and it worksjust fine – MetaSnarf Jul 24 '15 at 02:47

2 Answers2

8

Here's my implementation of moving the my location button:

<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="3">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:name="com.google.android.gms.maps.SupportMapFragment">
    </fragment>
      .
      .
      .

On my activity:

mapFragment = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.myMap));

 //if you're using a fragment use:
   //mapFragment = ((SupportMapFragment) getChildFragmentManager() .findFragmentById(R.id.myMap));


    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
           //add map adjustments here//


           //then this is where you move the my location button

      View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
            getParent()).findViewById(Integer.parseInt("2"));

    // and next place it, for exemple, on bottom right (as Google Maps app)
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
    // position on right bottom
    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    rlp.setMargins(0, 0, 30, 30);


        }
    });
MetaSnarf
  • 5,857
  • 3
  • 25
  • 41
  • I was initially very hopeful about this when I changed my code to be the same, compiled, and saw that the mylocation button was appearing in the bottom right-hand corner even though I couldn't understand from the code what could cause it to go to the right. However, every attempt I made to modify your code to make the button appear in the left did not succeed. I can only move it from the top right to the bottom right using this code - which leaves me sadly with the same issue -> not being able to show the button in the topleft on api 16. However, many thanks for trying to help – Jon Jul 24 '15 at 22:46
  • how about creating your own location button and adding it on the frame layout. That way, you'll never find a problem moving it anywhere on the screen. – MetaSnarf Jul 25 '15 at 09:43
0

one way, unfortunately, is to create it from scratch

        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams((int)(45*density),(int)(45*density));
        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlp.setMargins((int)(5*density), 0, 0, (int)(5*density));
        locationButton.setLayoutParams(rlp);

where density is, for example,

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    dispWidth = metrics.widthPixels;
    dispHeight = metrics.heightPixels;
    density = metrics.scaledDensity;
djdance
  • 3,110
  • 27
  • 33