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?
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With