3

I'm using the Google Maps API for Android. The following code enables the maps toolbar in the bottom right corner when I click on a Marker

googleMap.getUiSettings().setMapToolbarEnabled(true); 

However, I'd like this maps toolbar to appear in the TOP RIGHT corner of the map, rather than the default bottom right position.

How would I do this?

Red Ghost
  • 302
  • 2
  • 12

3 Answers3

22

This is an answer that i came across before as to how to move the toolbar to the bottom of the page. I hope this helps you.

//get reference to my location icon
    View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
            getParent()).findViewById(Integer.parseInt("2"));

    // and next place it, for example, 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);

EDIT: As per suggestions (of @Andro) this is the code to change the location of the map toolbar:

//get reference to my location icon

 View toolbar = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).
            getParent()).findViewById(Integer.parseInt("4"));

    // and next place it, for example, on bottom right (as Google Maps app)
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) toolbar.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
  • 1
    `View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")). getParent()).findViewById(Integer.parseInt("4")); ` for maps toolbar – Andro Oct 29 '15 at 08:21
  • Thanks man, but you should edit your answer as @Andro suggest – Antwan Oct 31 '15 at 13:28
  • why `parseInt("4")` ? – MetaSnarf Nov 02 '15 at 02:07
  • 1
    The mapView has different IDs for different views inside it. That is, different IDs for google text, map toolbar, location button etc. Toolbar has the ID 4. ID 2 is for location icon. :) – Andro Nov 02 '15 at 06:25
  • ohw,.right, thanks for the explanation. I'll change my answer right away, – MetaSnarf Nov 02 '15 at 06:28
  • This looks and feels extremely hacky. IMO you should never touch internal views this way because it may/will break on future releases of the library. – mradzinski Feb 13 '17 at 14:47
  • not working with parseInt("4") only work with parseInt("2") – Amar Singh Apr 13 '20 at 08:04
  • 1
    Works great, thank you! Later versions of API started asking "not enough information" for findViewById. To resolve it: https://stackoverflow.com/questions/45267041/not-enough-information-to-infer-parameter-t-with-kotlin-and-android – Liker777 Jan 09 '22 at 12:18
5

Thought of commenting, But has low reputation. So:

As far as I know, there is no way to position it for now. It'll appear at its default position only.

As Nathan said I should add some alternative if possible. So,There is a work around: You can disable the map's toolbar. and can add appcompact's ToolBar anywhere in the layout.

Reference : https://developer.android.com/reference/android/support/v7/widget/Toolbar.html

  • 2
    I would recommend, if possible, changing this to say something along the lines of "You can't, but here's something you can do." – Nathan Tuggy Feb 24 '15 at 05:32
  • Of course, But there must be _something_, he can do about it. –  Feb 24 '15 at 05:38
  • Right. If you can explain what that something might be, then this would be a perfectly legitimate answer. Otherwise, not so much. – Nathan Tuggy Feb 24 '15 at 05:39
  • What I meant by that is, there is nothing to do about changing position of existing toolbar UI of Google map. Hope that makes sense now. :) –  Feb 24 '15 at 05:41
  • It does, but unfortunately that does mean there's probably no answer anyone can give, if there simply is no workaround or partial solution or hack that can address the issue at all. – Nathan Tuggy Feb 24 '15 at 05:42
  • There can be a work around, But I answered what OP asked. That's all. –  Feb 24 '15 at 05:46
  • In digging up a reference to explain what I was recommending, I ran across [this Meta question](http://meta.stackoverflow.com/questions/261168/is-this-is-not-possible-an-acceptable-answer), which appears to have rather more useful guidance. – Nathan Tuggy Feb 24 '15 at 05:54
0

UPDATE 2021

You should be able to move map UI controls (compass, my location, zoom, etc) by settings padding on your googleMap object. That's the only provided internal way as I found. Here's info from docs:

Padding can be helpful when designing UIs that overlap some portion of the map. For example, in the below image, the map is padded along the top and right edges. Visible map controls and legal text will be displayed along the edges of the padded region

enter image description here

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

Neone
  • 482
  • 1
  • 9
  • 21