How To Implement Banner ads in your android app

 

  Banner ads implementation in  android app -

Step 1 - Go to Manifest file  add internet  permission 
<uses-permission android:name="android.permission.INTERNET"/>
and ads unit in application tag 

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

Step 2 - Go to build.gradle file and add dependencies

implementation 'com.google.android.gms:play-services-ads:20.1.0'
 Step 3- Go to Xml file and paste this code
<com.google.android.gms.ads.AdView
android:id="@+id/banner_ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111" />



Step 4 - Write these lines of code in java file 
first before onCreate Method 
private  AdView bannerAdView;

after this in OnCreate Method Write this code

//<--------------------- Banner adsView - Starts Here--------------------------->//
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});

bannerAdView = findViewById(R.id.banner_ads);
AdRequest adRequest = new AdRequest.Builder().build();
bannerAdView.loadAd(adRequest);

bannerAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}

@Override
public void onAdFailedToLoad(LoadAdError adError) {
bannerAdView.loadAd(adRequest);

}

@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}

@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}

@Override
public void onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
});
//<--------------------- Banner adsView - Ends Here--------------------------->//

Post a Comment

Previous Post Next Post