3

So, I implemented AdMob ads with the Play Services SDK. I've done everything 'by the book', but the ads won't show.

If I set the AdView background to white, it shows the white space but not the Ad. I'm using Fragments, but I'm putting the AdView in the activity_main.xml (although in the fragments I've set a margin of 60dp at the bottom)

Here's my onCreate code (with the actual code and the 'testing' code)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

    drawer.openDrawer(Gravity.LEFT);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    AdView adView = (AdView)this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("Device_ID")
            .build();
    adView.loadAd(adRequest);
}

And here's my activity_main.xml

<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.MainActivity">


<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/adView" >

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adUnitId="Unit_ID"
        ads:adSize="BANNER"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

What am I missing?

surubutna
  • 87
  • 2
  • 7

1 Answers1

5

I think in addition to this block that you already have:

AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("Device_ID")
        .build();

...You are missing something like this:

    AdView adView = (AdView) this.findViewById(R.id.adView);
    adView.loadAd(adRequest);

Here is complete picture:

  1. what is in xml file:

    <com.google.android.gms.ads.AdView android:id="@+id/adView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      ads:adUnitId="your ad unit id"
      ads:adSize="BANNER"/>
    
  2. what is in onCreate:

    AdRequest adRequest = new AdRequest.Builder()
            .build();
    
    AdView adView = (AdView) this.findViewById(R.id.adView);
    adView.loadAd(adRequest);
    

Please note the code above is for production. You must use test device id for testing in order to avoid AdMob penalties.

NeviQ-OR
  • 305
  • 2
  • 12
  • 1
    I changed the code to `AdView adView = (AdView)this.findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("Device_ID") .build(); adView.loadAd(adRequest);` but it still doesn't show up :/ – surubutna May 24 '14 at 18:50
  • I'm assuming you've used actual device id from logcat, not Device_ID string - right? As well as actual adUnitId is used, not Unit_ID string. – NeviQ-OR May 24 '14 at 18:54
  • Obviously ahah (but I understand your question) – surubutna May 24 '14 at 18:57
  • Just make sure you are not confusing adUnitId and your publisher id. publisher id starts with pub-. This was my mistake when i first integrated admob. – chandings May 24 '14 at 19:09
  • Nop, it's still not that. I took my Unit ID from the logcat (when it advises me to use the 'test code'). It reservates space for the ad, but for some reason the ad doesn't show :/ – surubutna May 24 '14 at 19:22
  • Assuming all mentioned IDs in place and you're using emulator, please check logcat for "Ad finished loading" string. If it's there, I think your layout is hiding something. – NeviQ-OR May 24 '14 at 19:23
  • I don't get that message, I only get this `Ad is not visible. Not refreshing ad.` – surubutna May 24 '14 at 19:40
  • To be honest I don't understand your logic behind keeping layout android:layout_above="@+id/adView" and keeping android:id="@+id/adView" part of the same layout. Can you change layout settings and see whether it has any impact? – NeviQ-OR May 24 '14 at 19:52
  • Makes sense. I've changed the layout but it still doesn't show. I think the key thing here is the logcat message `Ad is not visible. Not refreshing ad.`, but couldn't find any solution to my problem in the threads I've found – surubutna May 24 '14 at 20:45
  • Normally I also receive Ad is not visible (When, for example, activity is on pause) but only after "Ad finished loading". – NeviQ-OR May 24 '14 at 20:49
  • i am receiving message "failed to load ad: 1" in my logcat. can anyone help me with this – Abhinav Raja Aug 14 '14 at 17:18