AdMob Ad Implementation 2024 in Java || Banner, Interstitial Ad
AdMob Ad Implementation 2024 in Java || Banner, Interstitial Ad:
How to implement AdMob Banner and Interstitial Ads in Android Studio in Java 2024. In this blog you will learn it.
Admob Library Implementation
Go setting.gradle and check its available or not?
allprojects {
repositories {
google()
mavenCentral()
}
Go gradle.properties and Add this code
android.enableJetifier=true
Go build.gradle(Module App) and Add this dependencies.
implementation 'com.google.android.gms:play-services-ads:23.1.0'
Go to Manifest and Add Internet Permission above Application tag || Then Add Meta data in Application tag
<uses-permission android:name="android.permission.INTERNET"/>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713
"/>
Initialize Admob in your Activity
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
})
.start();
Banner Ads
Step 1 : Implementation Admob banner ads method in your MainActivity
// Banner Ads
// Step 1 : Implementation Admob banner ads method in your Activity
private void loadBanner(LinearLayout adViewContainer) {
// Create a new ad view.
AdView adView = new AdView(this);
adView.setAdSize(getAdSize(adViewContainer));
adView.setAdUnitId("ca-app-pub-3940256099942544/9214589741");
// Replace ad container with new ad view.
adViewContainer.removeAllViews();
adViewContainer.addView(adView);
// Start loading the ad in the background.
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
private AdSize getAdSize(LinearLayout adViewContainer) {
// Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = outMetrics.density;
float adWidthPixels = adViewContainer.getWidth();
// If the ad hasn't been laid out, default to the full screen width.
if (adWidthPixels == 0) {
adWidthPixels = outMetrics.widthPixels;
}
int adWidth = (int) (adWidthPixels / density);
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
Step 2 : Create a adViewContainer in your xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<!-- ***** START : Main Body - Do Anything ***** -->
<Button
android:id="@+id/btnInterstitialAds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Interstitial Ads"
android:backgroundTint="@color/main_color2"
android:textColor="@color/white"
android:layout_centerInParent="true" />
<!-- ***** END : Main Body - Do Anything ***** -->
<!-- Step 2 : Create a adViewContainer in your xml file ***** -->
<!-- ***** START : Banner Ad Container ***** -->
<LinearLayout
android:id="@+id/adViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
>
</LinearLayout>
<!-- ***** END : Banner Ad Container ***** -->
</RelativeLayout>
Step 3 : Show Banner Ads
// Banner Ads
// Step 3 : Finding adViewContainer and call loadBanner() method
LinearLayout adViewContainer = findViewById(R.id.adViewContainer);
loadBanner(adViewContainer);
// Banner Ads
// Step 3 : Finding adViewContainer and call loadBanner() method
LinearLayout adViewContainer = findViewById(R.id.adViewContainer);
loadBanner(adViewContainer);
Interstitial Ads
Step 1 : Create a public variable Then create loadFullscreenAd() method in your Activity in Java
InterstitialAd mInterstitialAd;
// FullScreen Ads
// Step 1 : loadFullscreenAd
private void loadFullscreenAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdClicked() {
}
@Override
public void onAdDismissedFullScreenContent() {
mInterstitialAd = null;
loadFullscreenAd();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
mInterstitialAd = null;
}
@Override
public void onAdImpression() {
}
@Override
public void onAdShowedFullScreenContent() {
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
}
});
}
// loadFullscreenAd
Step 2 : Show Interstitial Ads
// FullScreen Ads
// Step 2 : Finding btnInterstitialAds and call InterstitialAds() method
Button btnInterstitialAds = findViewById(R.id.btnInterstitialAds);
loadFullscreenAd();
btnInterstitialAds.setOnClickListener(v -> {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
}
});
Thanks from
Learn with Debasish