This is just a five step process:
1) Add buildscript in build.gradle (project)
2) Add the dependency of Admob in build.gradle (module)
3) Initialize ads,
4) You only have to create an object of AdvertiseUtil.java class which has everything set,
5) Paste the banner ad's small code in your layout,
6) Just call the banner or interstitial ad whichever you want.
You are done!
------------------------------------------ build.gradle (project) ----------------------------------
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
}
plugins {
alias(libs.plugins.androidApplication) apply false
}
------------------------------------------------------------------------
------------------------------------------ build.gradle (module) ----------------------------------
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.app.yourappname"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.google.android.gms:play-services-ads:23.1.0'
}
------------------------------------------------------------------------
-------------------------------- string.xml ---------------------------
<resources>
<string name="app_name">WhatAnAndroid</string>
<!-- Admob Test Ads-->
<string name="ad_app_id">ca-app-pub-3940256099942544~3347511713</string>
<string name="ad_native">ca-app-pub-3940256099942544/2247696110</string>
<string name="ad_interstitial">ca-app-pub-3940256099942544/1033173712</string>
<string name="ad_banner">ca-app-pub-3940256099942544/6300978111</string>
<string name="ad_rewarded">ca-app-pub-3940256099942544/5224354917</string>
<!-- Admob Test Ads-->
</resources>
-------------------------------------------------
------------------------------ admob_ads_activity.xml -----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_rll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary">
<RelativeLayout
android:gravity="center"
android:id="@+id/aaa_topBannerAdViewRL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<RelativeLayout
android:id="@+id/aaa_bottomBannerAdViewRL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</RelativeLayout>
</RelativeLayout>
-------------------------------------------------
------------------------------------- AdvertiseUtil.java ---------------------------------
import android.app.Activity;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.UnstableApi;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.prashantsj.apps.AdmobAdsActivity.R;
public class AdvertiseUtil {
private Context context;
private InterstitialAd mInterstitialAd;
public AdvertiseUtil(Context context) {
this.context = context;
}
/********************BANNER***********************/
private AdView adView;
public AdView loadBannerAd() {
adView = new AdView(context);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(context.getString(R.string.ad_bannerad));
showBanner();
return adView;
}
public void showBanner() {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
public void onResumeBanner(){
if(adView!=null)
adView.resume();
}
public void onPauseBanner(){
if(adView!=null)
adView.pause();
}
public void onDestroyBanner(){
if(adView!=null)
adView.destroy();
}
/*********************************************/
/*********************INTERSTITIAL*************************/
public void loadInterstitialAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this.context, context.getString(R.string.ad_interstitial), adRequest,new InterstitialAdLoadCallback() {
@OptIn(markerClass = UnstableApi.class) @Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.i("Interstitial Ad Loaded", "onAdLoaded");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
// Log.i("", loadAdError.toString());
mInterstitialAd = null;
}
});
}
public void showInterstitialAd() {
if (mInterstitialAd != null) {
mInterstitialAd.show((Activity) context);
loadInterstitialAd();
}
}
/*******************************************************/
}
-------------------------------------------------
------------------------------------- AdmobAdsActivity.java --------------------------
package com.app.yourappname;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.app.yourappname.AdvertiseUtil;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class AdmobAdsActivity extends AppCompatActivity {
private AdvertiseUtil au;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admob_ads_activity);
MobileAds.initialize(this, initializationStatus -> {});
au = new AdvertiseUtil(this);
RelativeLayout topBanner = findViewById(R.id.aaa_topBannerAdViewRL);
RelativeLayout bottomBanner = findViewById(R.id.aaa_bottomBannerAdViewRL);
topBanner.addView(au.loadBannerAd());
bottomBanner.addView(au.loadBannerAd());
au.loadInterstitialAd();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
if(au!=null) au.showInterstitialAd();
}
@Override
protected void onPause() {
super.onPause();
if(au!=null) au.onPauseBanner();
}
@Override
protected void onResume() {
super.onResume();
if(au!=null) au.onResumeBanner();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(au!=null) au.onDestroyBanner();
}
}
-------------------------------------------------
According to the new admob implementation rules, you need to add the app id in manifes
-------------------------- Declare the Activity name and internet permissions in manifest file-----------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application<meta-dataandroid:hardwareAccelerated="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:targetApi="31">
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/ad_app_id"/>
<activity android:name=".AdmobAdsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>