How to create an ADMOB App Open Ad in Android Studio | Google Admob App Open Ad Network | Admob App Open Ad Network

In this article, we have to create an AdMob interstitial ad format that is used to implement the bottom of the layout or else the top of the layout. So, we have to implement the below code in your project and follow it step by step.

STEP: 01

We can insert dependency in your build.gradle(Module: level) given below.

// Admob Ads Dependency
implementation 'com.google.android.gms:play-services-ads:19.6.0'

STEP: 02

Insert Meta-Deta in your “manifests” file given below.

<!-- 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: 03

We have to insert INTERNET permission to run this banner ad given below.

<uses-permission android:name="android.permission.INTERNET" />

STEP: 04

we have to create and add a “HardwareAccelerated” system in the manifests file that helps to show Rewarded Interstitial Ad perfectly on your mobile device.

android:hardwareAccelerated="true"

STEP: 05

Now, you can be implemented the above dependency then you have to move coding in java files. So First we have to create a JAVA file named “MyApplication” then you have to insert the below code in your project.

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

MobileAds.initialize(this, initializationStatus -> {
});
AppOpenManager appOpenManager = new AppOpenManager(this);
}
}

After that, we have to create one more JAVA file named “AppOpenManager” then you can insert all java code into your project.

public class AppOpenManager implements Application.ActivityLifecycleCallbacks {

private Activity currentActivity;
private static boolean isShowingAd = false;
private static final String LOG_TAG = "AppOpenManager";
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294";
private AppOpenAd appOpenAd = null;
private long loadTime = 0;
private final MyApplication myApplication;

// Constructor
public AppOpenManager(MyApplication myApplication) {
this.myApplication = myApplication;
this.myApplication.registerActivityLifecycleCallbacks(this);
}

// Request an Ads
public void fetchAd() {
if (isAdAvailable()) {
return;
}

AppOpenAd.AppOpenAdLoadCallback loadCallback = new AppOpenAd.AppOpenAdLoadCallback() {
@Override
public void onAppOpenAdLoaded(AppOpenAd ad) {
AppOpenManager.this.appOpenAd = ad;
AppOpenManager.this.loadTime = (new Date()).getTime();
}

@Override
public void onAppOpenAdFailedToLoad(LoadAdError loadAdError) {

}

};
AdRequest request = getAdRequest();
AppOpenAd.load(
myApplication, AD_UNIT_ID, request,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);

}

// Creates and returns ads request
private AdRequest getAdRequest() {
return new AdRequest.Builder().build();
}

// Utility method to check if ad was loaded more than n hours ago.
private boolean wasLoadTimeLessThanNHoursAgo() {
long dateDifference = (new Date()).getTime() - this.loadTime;
long numMilliSecondsPerHour = 3600000;
return (dateDifference < (numMilliSecondsPerHour * (long) 4));
}

// Check if ad exists and can be shown
public boolean isAdAvailable() {
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo();
}

@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

}

@Override
public void onActivityStarted(@NonNull Activity activity) {
currentActivity = activity;
showAdIfAvailable();

}

@Override
public void onActivityResumed(@NonNull Activity activity) {
currentActivity = activity;
}

@Override
public void onActivityPaused(@NonNull Activity activity) {

}

@Override
public void onActivityStopped(@NonNull Activity activity) {

}

@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

}

@Override
public void onActivityDestroyed(@NonNull Activity activity) {
currentActivity = null;

}

// Shows the ad if one isn't already showing.
public void showAdIfAvailable() {
if (!isShowingAd && isAdAvailable()) {
Log.d(LOG_TAG, "Will show ad.");

FullScreenContentCallback fullScreenContentCallback =
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
AppOpenManager.this.appOpenAd = null;
isShowingAd = false;
fetchAd();
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
}

@Override
public void onAdShowedFullScreenContent() {
isShowingAd = true;
}
};

appOpenAd.show(currentActivity, fullScreenContentCallback);

} else {
Log.d(LOG_TAG, "Can not show ad.");
fetchAd();
}
}
}
Scroll to Top