Search code examples
javaandroidadmob

Android AdView not visible - but clickable


I have created an Android app with the new ads sdk, but it is invisible. I got it working in a previous app, it works good there. Now, in my new app, I have done the same. But:

The AdView is not visible, but clickable. If you go back to your home screen (just minimize, dont stop!), and re-open it, the ad will be shown.

My XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >

<org.andengine.opengl.view.RenderSurfaceView
    android:id="@+id/SurfaceViewId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:gravity="center" />

<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adViewId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adUnitId="XXX"
    ads:adSize="BANNER" />

</RelativeLayout>

And my onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    adView = (AdView) findViewById(R.id.adViewId);

    AdRequest adRequest = new AdRequest.Builder().build();

    adView.loadAd(adRequest);

    v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}

EDIT: After 60 sec., the view becomes visible.


Solution

  • Your problem is that you have told you RenderSurfaceView to matchParent forits height. THis means that it will consume all of the space of its parent leaving none for the AdView.

    Try this instead:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >
    
    <org.andengine.opengl.view.RenderSurfaceView
        android:id="@+id/SurfaceViewId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />
    
    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adViewId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adUnitId="XXX"
        ads:adSize="BANNER" />
    
    </LinearLayout>
    

    This uses layout_weight with LinearLayout to have your RenderSurfaceView expand to fill all unused vertical space.