Home » Android » Not showing ad with banner ad

Not showing ad with banner ad

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have used AdMob in my app but it doesn’t work and doesn’t show ad.

I’ve posted my code below.

Manifest.xml file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

build.gradle

compile 'com.google.android.gms:play-services-ads:9.8.0'

activity_main.xml

<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="@string/banner_home_footer"
        android:layout_marginBottom="12dp"
        android:layout_above="@+id/textView10"
        android:layout_alignLeft="@+id/textView10"
        android:layout_alignStart="@+id/textView10"
        android:layout_alignRight="@+id/btnSchedule"
        android:layout_alignEnd="@+id/btnSchedule">
</com.google.android.gms.ads.AdView>

string.xml

<string name="banner_home_footer">(Ad-Unit-ID)</string>

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    operationView();

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    //these codes are used for ads
    MobileAds.initialize(this,"App-ID");
    adView = (AdView) findViewById(adView);
    AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("ANCCC").build();
    adView.loadAd(adRequest);

    adView.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            // super.onAdClosed();
            Toast.makeText(getApplicationContext(),"closed",Toast.LENGTH_LONG).show();
            Log.i("Ads","onClosed");
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            // super.onAdFailedToLoad(errorCode);
            Toast.makeText(getApplicationContext(),"failed",Toast.LENGTH_LONG).show();
            Log.i("Ads","onAdFailedToLoad");
        }

        @Override
        public void onAdLeftApplication() {
            // super.onAdLeftApplication();
            Toast.makeText(getApplicationContext(),"left",Toast.LENGTH_LONG).show();
            Log.i("Ads","onAdLeftApplication");
        }

        @Override
        public void onAdOpened() {
            // super.onAdOpened();
            Toast.makeText(getApplicationContext(),"open",Toast.LENGTH_LONG).show();
            Log.i("Ads","onAdOpened");

        }

So, please help me what happening with my code, which affects in loading the ads in my app.

Answers: