In this article I will show you how to show banner ads using latest dependency.

Steps

Step 1: Add dependency in build.gradule file.

Step 2: Add meta data in android manifest file.

Step 3: Allow Internet Permission in android manifest file.

Step 4: Add XML code in layout.

Step 5: Add all imports in java class.

Just follow me.

Step 1:

Admob Banner Ad Dependency

implementation 'com.google.android.gms:play-services-ads:19.2.0'

Step 2:

Metadata of Admob Ads

<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>

Step 3:

Banner Ad XML Code

  <com.google.android.gms.ads.AdView
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true"
      ads:adSize="BANNER"
      ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
  </com.google.android.gms.ads.AdView>

Step 4:

Imports Of Java Code

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

Step 5:

Java Code

private AdView mAdView; 
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

Leave a Reply

Your email address will not be published. Required fields are marked *