I am trying to implement Admob in Android and I am seeing requests coming into AdMob. However, I am not seeing the Admob ads being displayed on the Android screen in the emu
I see there are two problems in your code based on what you posted:-
The <metadata>
section should contain the ADMOB_APP_ID instead of your
publisher ID. This should be declared under <application>
tag in
ApplicationManifest.xml.
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="[ADMOB_APP_ID]"/>
you can find ADMOB_APP_ID by on ADMOB dashboard, click on the application
and check "App Settings". You can see the APP_ID which starts
typically with ca-app-pub-0123456789012345.
The second problem is, where you have declared AdView in your layout.
Remember you have to provide Ad unit not your publisherID, which
you can create in ADMOB dashboard by clicking on Ad Unit tab
under your application. Put the correct "ad unit" against your
AdView as below.
ads:adUnitId="ca-app-pub-3940256099942544/6300978111" <!-- remember this is adUnit not App ID and this value above is for test banner Ad. -->
Once you have fixed above problems, do the following:-
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
in onCreate of your first Activity. This needs to be done only once and thus the right place is either your first activity or at application's onCreate callback.
Find the AdView in the activity onCreate where you have included AdView in the layout.
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Test Ads work by providing test adunit published by google.
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
Additionally, if you want to handle Ad events, do as follows:-
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
}
});
If admob "App Id" and "Unit Id" is correct then upload on google play store it will show the add. I was facing same type of issue but after upload on google play it resolve automatic.
This is because admin inventory requires some requests to be generated to check that the app can make some profit!
Please let setup the ads properly and publish the app, after about 1k requests are made, the ads will automatically show up!
I too was suffering from this problem and got my answer when I published my own app!
I had a similar issue. If you have any padding on your parent layout then you may not have enough width for the ads. If you have it in a portrait view try switching to a landscape view to see if it shows. If it does than you most likely have a width issue somewhere in your layout.
Some of these possible solutions may sound obvious, but make sure you have these completed:
-replace "My Publisher ID" in android:value="My Publisher ID" with your actual publisher ID.
-make sure to include the internet permission in your manifest file:
<uses-permission android:name="android.permission.INTERNET" />
If you have completed those, you can also try placing the following code in the "On create" section instead of your current:
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
adRequest.setTesting(true);
adView.loadAd(adRequest);
adView.loadAd(new AdRequest());
or
AdManager.setTestDevices( new String[] {AdManager.TEST_EMULATOR});
AdView adView = (AdView)findViewById(R.id.adView);
adView.requestFreshAd();
Before publishing, don't forget be sure to get rid of the setTestDevice though!
Be sure that your Admob layout is displaying in xml view. Put your admob view inside RelativeLayout and try to use android:alignparentBottom:true
<?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.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Do our code that you want to show in xml -->
<!--Put adview in bottom of screen -->
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
ads:adUnitId="@string/addmob_id"
ads:loadAdOnCreate="true"
ads:testDevices="HT9CWP803129" />
</RelativeLayout>
</LinearLayout>
In your java code put these lines in onCreate
method
// Load addvertisment
AdView adView = (AdView) findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder().addTestDevice("FF9CD441FA62FD456D7D571B91DD11DD").build();
adView.loadAd(adRequest);
This worked in my code, Hope it will help you too.