Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AdMob: addView doesn't show ad until return to activity

I'm trying to add a banner ad to the top of my game. My activity uses a RelativeLayout with a custom SurfaceView. I want the ad to overlap the SurfaceView.

The ad loads and is clickable, but is not drawn to the screen. When I leave the activity and return, the ad IS drawn (e.g. by pressing the home button and then returning to the app through the recents menu).

It's not a matter of waiting for the ad to load, as 1) the ad doesn't display if I wait, and 2) the ad is clickable even though it is not visible.

Here is my layout code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_mainmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.xxx.menu.SurfaceViewMainMenu
  android:id="@+id/view_mainmenu"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>


     <com.google.android.gms.ads.AdView
    android:id="@+id/ad"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/view_mainmenu"
    android:layout_alignLeft="@id/view_mainmenu"
    ads:adSize="BANNER"
    ads:adUnitId="@string/ad_unit_id_banner"/>

The code that loads the ad is posted as a Runnable to the UI thread by the RenderThread when the RenderThread is first run.

Does anyone have any ideas how I can make the ad show straight away? Or why the ad will only be drawn when I return to the activity?

I've tried calling forceLayout() / requestLayout() on the RelativeLayout but this doesn't help. I also tried calling invalidate() on the view.

Thanks, Tom.


EDIT

I added an AdListener to the AdView and can confirm that onAdLoaded is being called. The ad is loaded but not visible.

What works: I make another adLoad request in the onAdLoaded callback, and then the ad shows up.

So the ad only shows up after the second request I make. Anyone know what's going on here?

like image 369
TomEverin Avatar asked Feb 21 '14 12:02

TomEverin


1 Answers

I had the same issue, all I had to do was bring the adView to the front when it was loaded. (I recommend this over loading it twice)

mAdView.setAdListener(new AdListener()
{
    public void onAdLoaded()
    {
        Log.i("Ads", "onAdLoaded");
        mAdView.bringToFront();
    }
}
like image 94
Doolali Avatar answered Sep 22 '22 05:09

Doolali