15

I want to move Google map layout items at runtime, but I'm not able to do it and I don't know if it's possible or not. What I want to do is: I have a Google map fragment which takes up all the screen. I also have an Ad. When app starts, I show the map. Like this:

enter image description here

And when the ad loads, I show the ad at the bottom, but I need to move the Google logo above the ad. Like this:

enter image description here

I've tried with:

if (banner is loaded)
googleMap.setPadding(0, 0, 0, banner.getLayoutParams().height);

But I had no success. What am I doing wrong?

EDIT: Thanks to @SimplePlan, I changed the RelativeLayout where was included the mapfragment to a FrameLayout:

<FrameLayout >
    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <com.inmobi.monetization.IMBanner
        android:layout_gravity="bottom" />
</FrameLayout>

And I added a listener to the Ad:

banner.setIMBannerListener(new IMBannerListener() {
    @Override
    public void onBannerRequestSucceeded(IMBanner arg0) {
        googleMap.setPadding(0, 0, 0, banner.getLayoutParams().height);
    }
});
David
  • 901
  • 1
  • 13
  • 37
  • 2
    Put your `MapFragment` and Ad in `FrameLayout` and set `Ad gravity` as `Bottom` – M D Mar 08 '14 at 10:00
  • OK, it worked! I also had to set a banner listener (onBannerRequestSucceeded, I'm working with InMobi Ads) to know when the ad is loaded. – David Mar 08 '14 at 20:13

3 Answers3

17

As long as the layout process is not finished, the view's dimension is set to 0. Using Handler, you can set the map's padding after the layout is done.

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            mapHelper.map.setPadding(0,0,0,ad.getHeight());
        }
    });
sulai
  • 5,204
  • 2
  • 29
  • 44
  • 16
    Just to clarify the padding parameters are `setPadding(left, top, right, bottom)`. I only add this because when I look at the actual method it lists the parameter names as `i`, `i1`, `i2`, `i3` which is super unhelpful. – Markymark Apr 14 '17 at 01:18
  • @Markymark make sure to download "Sources for Android xx" in your Android SDK download manager to see javadoc and parameter names. – sulai Apr 04 '18 at 10:52
  • 1
    @sulai Sources for android does not include google map related code right? correct me if I am wrong. – amalBit Jan 17 '19 at 13:47
0

We can now use KTX extensions to do some view logic after view load complete:

https://developer.android.com/reference/kotlin/androidx/core/view/package-summary#(android.view.View).doOnLayout(kotlin.Function1)

Damrad
  • 46
  • 1
  • 4
0

Better way to do this is not to use FrameLayout and rather use RelativeLayout and put a relation that map should be above your banner. So whenever the banner appears your map will be accordingly moved up.

<RelativeLayout>
    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_above="@id/banner"
        />

    <com.inmobi.monetization.IMBanner
        android:id="@+id/banner"
        android:layout_gravity="bottom" />
</RelativeLayout>
Raj Suvariya
  • 1,592
  • 3
  • 14
  • 36