How To Show Interstitial Ad Every 20 Seconds In An Android Application

Introduction

Android is one of the most popular operating systems for mobile. In this article, I will show you how to show interstitial ads every 20 seconds in an Android application using Android Studio

Requirements

Steps to should be followed

Follow these steps to show interstitial ads every 20 seconds in an Android application. I have included the source code below.

Step 1

Open Android Studio and start a New Android Studio Project.

Android

Step 2

You can choose your application name and choose the location where your project is to be stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Android

Now, select the version of Android and select the target Android devices.

Android

Step 3

Now, add the activity and click "Next" button.

Android

Add Activity name and click "Finish".

Android

Step 4

Go to activity_main.xml. This XML file contains the designing code for your Android app.

Android

The XML code is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.    xmlns:tools="http://schemas.android.com/tools"  
  5.    android:layout_width="match_parent"  
  6.    android:layout_height="match_parent"  
  7.    tools:context="abu.interstitial_ad_every_20_seconds.MainActivity">  
  8.   
  9. <TextView  
  10.    android:layout_width="wrap_content"  
  11.    android:layout_height="wrap_content"  
  12.    android:text="Android Ad_Mob!"  
  13.    app:layout_constraintBottom_toBottomOf="parent"  
  14.    app:layout_constraintLeft_toLeftOf="parent"  
  15.    app:layout_constraintRight_toRightOf="parent"  
  16.    app:layout_constraintTop_toTopOf="parent" />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  

Step 5

Go to (gradle scripts ⇒ build.gradle(moduleapp) and add the below code.

Android
 
  1. apply plugin: 'com.android.application'  
  2. android {  
  3.     compileSdkVersion 26  
  4.     buildToolsVersion "26.0.1"  
  5.     defaultConfig {  
  6.         applicationId "abu.interstitial_ad_every_20_seconds"  
  7.         minSdkVersion 19  
  8.         targetSdkVersion 26  
  9.         versionCode 1  
  10.         versionName "1.0"  
  11.         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  
  12.     }  
  13.     buildTypes {  
  14.         release {  
  15.             minifyEnabled false  
  16.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
  17.         }  
  18.     }  
  19. }  
  20. dependencies {  
  21.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  22.     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {  
  23.         exclude group: 'com.android.support',  
  24.         module: 'support-annotations'  
  25.     })  
  26.     compile 'com.android.support:appcompat-v7:26.+'  
  27.     compile 'com.android.support.constraint:constraint-layout:1.0.2'  
  28.     testCompile 'junit:junit:4.12'  
  29.     compile 'com.google.android.gms:play-services-ads:10.2.4'  
  30. }  
Step 6

Go to Main Activity.java. This Java program is the backend language for Android app.

Android

The java code is given below.

  1. package abu.interstitial_ad_every_20_seconds;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.util.Log;  
  5. import android.widget.Toast;  
  6. import com.google.android.gms.ads.AdRequest;  
  7. import com.google.android.gms.ads.InterstitialAd;  
  8. import java.util.concurrent.Executors;  
  9. import java.util.concurrent.ScheduledExecutorService;  
  10. import java.util.concurrent.TimeUnit;  
  11. public class MainActivity extends AppCompatActivity {  
  12.     private InterstitialAd mInterstitialAd;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         prepareAd();  
  18.         ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();  
  19.         scheduler.scheduleAtFixedRate(new Runnable() {  
  20.             public void run() {  
  21.                 Log.i("hello""world");  
  22.                 runOnUiThread(new Runnable() {  
  23.                     public void run() {  
  24.                         if (mInterstitialAd.isLoaded()) {  
  25.                             mInterstitialAd.show();  
  26.                         } else {  
  27.                             Log.d("TAG"" Interstitial not loaded");  
  28.                         }  
  29.                         prepareAd();  
  30.                     }  
  31.                 });  
  32.             }  
  33.         }, 20, 20, TimeUnit.SECONDS);  
  34.     }  
  35.     public void prepareAd() {  
  36.         mInterstitialAd = new InterstitialAd(this);  
  37.         mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");  
  38.         mInterstitialAd.loadAd(new AdRequest.Builder().build());  
  39.     }  
  40. }  

Step 7

Now, go to the menu bar and click "make project" or press ctrl+f9 to debug the error.

Android

Step 8

Then, click "Run" button or press shift+f10 to run the project. Choose the "virtual machine" and click OK.

Android

Conclusion

We have successfully shown the interstitial ad every 20 seconds in an Android application using Android Studio.

Android

Android

Android

Up Next
    Ebook Download
    View all
    Learn
    View all