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.
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.
Now, select the version of Android and select the target Android devices.
Step 3
Now, add the activity and click "Next" button.
Add Activity name and click "Finish".
Step 4
Go to activity_main.xml. This XML file contains the designing code for your Android app.
The XML code is given below.
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="abu.interstitial_ad_every_20_seconds.MainActivity">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Android Ad_Mob!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- </android.support.constraint.ConstraintLayout>
Step 5
Go to (gradle scripts ⇒ build.gradle(moduleapp) and add the below code.
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 26
- buildToolsVersion "26.0.1"
- defaultConfig {
- applicationId "abu.interstitial_ad_every_20_seconds"
- minSdkVersion 19
- targetSdkVersion 26
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
- exclude group: 'com.android.support',
- module: 'support-annotations'
- })
- compile 'com.android.support:appcompat-v7:26.+'
- compile 'com.android.support.constraint:constraint-layout:1.0.2'
- testCompile 'junit:junit:4.12'
- compile 'com.google.android.gms:play-services-ads:10.2.4'
- }
Step 6
Go to Main Activity.java. This Java program is the backend language for Android app.
The java code is given below.
- package abu.interstitial_ad_every_20_seconds;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
- import com.google.android.gms.ads.AdRequest;
- import com.google.android.gms.ads.InterstitialAd;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- public class MainActivity extends AppCompatActivity {
- private InterstitialAd mInterstitialAd;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- prepareAd();
- ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
- scheduler.scheduleAtFixedRate(new Runnable() {
- public void run() {
- Log.i("hello", "world");
- runOnUiThread(new Runnable() {
- public void run() {
- if (mInterstitialAd.isLoaded()) {
- mInterstitialAd.show();
- } else {
- Log.d("TAG", " Interstitial not loaded");
- }
- prepareAd();
- }
- });
- }
- }, 20, 20, TimeUnit.SECONDS);
- }
- public void prepareAd() {
- mInterstitialAd = new InterstitialAd(this);
- mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
- mInterstitialAd.loadAd(new AdRequest.Builder().build());
- }
- }
Step 7
Now, go to the menu bar and click "make project" or press ctrl+f9 to debug the error.
Step 8
Then, click "Run" button or press shift+f10 to run the project. Choose the "virtual machine" and click OK.
Conclusion
We have successfully shown the interstitial ad every 20 seconds in an Android application using Android Studio.