Search code examples
javaandroidgoogle-maps-api-2

Android Google Maps: adequately adjust map padding for different device sizes


I have a toolbar in front of my MapsFragment, so the padding of the map controls need to be adjusted accordingly. However, when I go by screenLayout there are still discrepancies between various tablet devices. Within the SCREENLAYOUT_SIZE_XLARGE there are still differences in output on the screen. See differences between "My Location"-button in screenshots below

How should I calculate the correct MapPadding for the My Location button?

@Override
public void onMapReady(GoogleMap googleMap) {
  gMap = googleMap;
  int screenSize = (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
  System.out.println("screenSize: " + screenSize);
  if (screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
    googleMapPadding = 75; //125 for other tablet
  } else if (screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE) {
    googleMapPadding = 100;
  } else {
    googleMapPadding = 175;
  }

  gMap.setPadding(0, googleMapPadding, 0, 0);

  //other stuff
}

XLarge

XLarge 2


Solution

  • Basically it was just calculating the height of the actionbar.

     TypedValue tv = new TypedValue();
        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        {
          googleMapPadding = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
        }
    
        gMap.setPadding(0, googleMapPadding, 0, 0);