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?
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:
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"/>
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.
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