I am using admob in my app, it was working fine but suddenly it stopped showing ads at all. Bellow is my codes i used:
mainActivity.xml:
In my case the ad didnt show up until I gave enough room for it.
Before it was like
<LinearLayout
android:orientation="vertical"
android:background="@color/primary"
android:layout_width="match_parent"
android:layout_height="90dip">
<ImageView
android:onClick="goBack"
android:clickable="true"
android:src="@drawable/ic_arrow_back_white_48dp"
android:layout_width="50dip"
android:layout_height="50dip"/>
<com.google.android.gms.ads.AdView
android:layout_gravity="center_horizontal"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/dwn_bnr_ad"/>
Here the adview is having height of wrap_content, so it adjusts itself to achieve its height, but its parent (LinearLayout) has a strict height of 90dip which doesnt allow the adView to take enough space to display ads.
So what I did was just changed the height of the LinearLayout to wrap_content then things got fine
Use below lines of code
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // My Galaxy Nexus test phone
.build();
mAdView.loadAd(request );
instead of using this
AdRequest adRequest = new AdRequest.Builder().build();
because this line will be used when your application would be ready for publish on Google Play Store.
You can place your device's id in place of "AC98C820A50B4AD8A2106EDE96FB87D4". Code to get device id:
String android_id = Settings.Secure.getString(getContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
Please read these carefully:
https://developers.google.com/admob/android/targeting#test_ads
https://developers.google.com/admob/android/quick-start
See this
Did you go back to your admob account to register your specific app for ads and get a new longer publisher number with the 'ca-app-pub-' preface instead of the 'pub-' preface?
Nowhere in the "banner ads 1" instructions on the Google Admob "Google Mobile Ads SDK" development site does it mention having to go back to your admob account to do this.
This stupid mistake held me up for days.
In my case, I had the code below under the buildscript
section of the project level build.gradle
Make sure maven {url "https://maven.google.com"}
is under allProjects > repositories
:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Put the banner in a LinearLayout just as this ,i faced the same problem and i fixed it by puting it in a linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-8056472942589154/2652831823"
android:layout_alignBottom="@+id/saida"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
Check if you have latest versions of adMob and GooglePlayServices lib.