Arsh Kapoor
Arsh Kapoor

Reputation: 13

Admob Banner Ad not loading in Android

My Admob Banner ad in not loading.

Toast displays that "Banner add is loaded".

I tried many other solutions provided on stackoverflow but none seemed to work for me.

MainActivity.java

    public class MainActivity extends AppCompatActivity {

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

        mBannerAd = (AdView) findViewById(R.id.banner_ad);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("A5E3E2068BD88202CBC281AD7XXXXXXX")
                .build();
        mBannerAd.loadAd(adRequest);
        mBannerAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                Toast.makeText(getApplicationContext(), "Closing the Banner Ad", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onAdLoaded() {

                Toast.makeText(getApplicationContext(), "Banner Ad is loaded", Toast.LENGTH_LONG).show();
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.akapoor.shayri.MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/banner_ad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3180111609438244/24179XXXXX" />

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akapoor.shayri">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ShayariText"></activity>
    </application>

</manifest>

Upvotes: 1

Views: 922

Answers (3)

niceumang
niceumang

Reputation: 1431

Initialize MobileAds in onCreate() like this:

MobileAds.initialize(MainActivity.this,"YOUR_APP_ID));

Upvotes: 0

Daniel Storm
Daniel Storm

Reputation: 18898

Your code looks fine. You should remove the test devices, addTestDevice, though:

Change:

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

to:

AdRequest adRequest = new AdRequest.Builder().build();

Upvotes: 0

Abhiroj Panwar
Abhiroj Panwar

Reputation: 528

Assuming that:-

  • You have complete the steps mentioned here to display a test ad successfully

  • This is not a test ad but an actual ad unit.

  • You have replaced test ad unit ID with the actual ad unit ID

If these steps are done, then It will take an hour or two to get your ad unit displayed in the app.

Upvotes: 1

Related Questions