0

I have a relative layout which contains:

  1. adView which is aligned at the parent layout's bottom. another
  2. Relative layout containing the app view like buttons and others.

As known, ads are sometimes not available or the wifi is off hence it is not wise to leave the adview space empty while the ad is not available. Hence, I want the inside relative layout to automatically shift up when the ad banner appears. How may I do so?

3 Answers3

1

What I did to solve this problem was embed the adview inside an otherwise empty linearlayout, with the AdView defaulted to visibility:gone. I added an AdListener to the AdView and set the visibility to "visible" in the onAdLoaded method in the AdListener.

    <LinearLayout android:id="@+id/ad_wrapper"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <AdView android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            android:visibility="gone"/>

    </LinearLayout>

and then

    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            adView.setVisibility(View.VISIBLE);
        }
    });
0

I would suggest embedding your RelativeLayout inside a LinearLayout, set the LinearLayout orientation to vertical, like :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- Your Ad view -->

    <RelativeLayout ....>
           <!-- items -->
    </RelativeLayout>

</LinearLayout>

if you set within your code the adview to View.GONE, the placement in the LinearLayout will be displaced, and the below content (RelativeLayout) will shift up.

Antoine Baqain
  • 392
  • 1
  • 3
0

Why do you say "it is not wise to leave the adview space empty while the ad is not available"?

There is absolutely nothing wrong with doing that and leaving it visible can often make your layout easier to manage.

But if you really want to hide it when no ad is available then use the AdListener method to set the visibility on the AdView.

William
  • 20,150
  • 8
  • 49
  • 91