Reputation: 95
I don't get what I'm doing wrong, I wanted to show a banner on the bottom but not even the test ads show up. I followed their tutorial and my app looks like this:
xml with the ad (it does show up on the preview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/infoPageBackgroundColor"
android:orientation="vertical"
android:paddingTop="8dp"
tools:context=".AppInfoActivity">
...
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:adSize="SMART_BANNER"
app:adUnitId="@string/banner_id"/>
Metadata in AndroidManifest
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/app_id"/>
Project gradle.build
buildscript {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
...
}
}
App gradle.build
dependencies {
...
}
Entry Activity
import com.google.android.gms.ads.MobileAds;
public class EntryActivity extends BaseActivity {
private View progressBar;
private LoadListAsyncTask loadListAsyncTask;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
MobileAds.initialize(this, "@string/app_id");
}
Java file
...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
...
public class StickerPackListActivity extends BaseActivity {
...
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
...
I'm using the banner test ID to try it and it only shows up in the xml editor. Any idea of why? Even if I try my ID it deosn't work. Sorry for not being able to explain much more as it's just like this. Did I forget something?
Upvotes: 3
Views: 7880
Reputation: 1773
You forgot to initialise AdMob:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
MobileAds.initialize(this, "BANNER_ID_HERE");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
...
}
https://developers.google.com/admob/android/banner
Recently created AdMob banners sometime fail to load with error code 2 when tested on physical devices. You should always test them on an emulator to ensure everything has been done correctly, seeing as there is nothing wrong with your code.
Upvotes: 4
Reputation: 2383
You need to initialize the Ads SDK:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
// Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
}
https://firebase.google.com/docs/admob/android/quick-start
Also make sure that your app id and banner id are correct:
https://support.google.com/admob/answer/7356431?hl=en
Upvotes: 0