Ercksen
Ercksen

Reputation: 669

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.

Upvotes: 1

Views: 583

Answers (3)

Saket Milan
Saket Milan

Reputation: 103

Check if there is any foreground set to your parent layout in which you are loading adview. In my case there was a foreground color set and on fast scrolling of recyclerview, foreground color was being set over AdView.

<LinearLayout
    android:id="@+id/ll_strip_ad"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="vertical"
    android:foreground="?attr/window_background_attr" //on commenting this line issue resolved.
    app:layout_constraintEnd_toEndOf="parent">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adViewId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adUnitId="XXX"
        ads:adSize="BANNER" />

</LinearLayout>

Upvotes: 0

William
William

Reputation: 20196

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.

Upvotes: 2

Angelo Cassano
Angelo Cassano

Reputation: 180

Maybe you forgot implementing these important methods

public void onPause() {
    super.onPause();
    this.adView.pause();
}

public void onResume() {
    super.onResume();
    this.adView.resume();
}

Upvotes: 0

Related Questions