How to integrate Android Admob Interstitials Recommendation (App load and exit)

In this tutorial, we are going to learn how to integrate Android Admob Interstitials Ad without violating Google Admob Ad Polices.

As a developer or even android app hobbyist, most developers will like to make money from their app. Although, there are many ways one can make money from an app but this tutorial will focus on Google Admob advertisement.

If you are starting out with mobile advertisement, you can read more about Google Admob and how you can set-up an account in Google Admob support.

First, we will focus on how best to implement Google Admob Interstitials Ad in the application we will develop now. The app will focus on App load and exit which Google has a guideline that every app with Admob ad serving must conform to.

If you are using Android Studio, the IDE has already made template for Admob integration but this tutorial will give you an understanding of how things work under the hood.

After we finish with the first example, we will move ahead to illustrate how to avoid repeating or recurring interstitials ad in your app.

Before we dive into more details, it is important for us to understand what we are planning to achieve. Below is the screen-shot of the application we will be creating.

admobwrong

admobguide

 

What we will do

android wall ad

Lets start to soil our hands in code. Start up your IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.

Windows 10

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 23

To create a new android application project, follow the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidAdmob

Package: com.inducesmile.androidadmob

Select Blank Activity

Name your activity : MainActivity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.

Strings.xml


We are going to update our project strings.xml file located in the values folder inside the res folder. Open the file and add the code below to it. In the ad_unit_id, make sure you add your admob id.

<resources>
    <string name="app_name">Android Admob</string>
    <string name="banner_ad_unit_id"> </string>
    <string name="welcome">Welcome to Home Screen</string>
    <string name="intro_screen">Intro Screen</string>
</resources>

Colors.xml

Open the colors.xml file in the same location as the strings.xml file and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

 

Manifest.xml

Since Google Admob requires network call, we are going to add a permission to use internet.

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

We will override the default Android Application class. Then we will set the Application name attribute in Application section in Manifest.xml file to the new class name (AdmobApplication).

Open your Manifest.xml file and add the code below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.inducesmile.androidadmob">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name=".AdmobApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".HomeScreenActivity"></activity>
    </application>
</manifest>

 

activity_main.xml

The main layout file that serves as the intro interface layout for this application will contain a single TextView control with text “Intro screen”. This page will work like a splash screen. After 5 seconds of load, it will display a interstitial ad if it has been loaded.

Open this layout file and add the code below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.inducesmile.androidadmob.MainActivity">
   
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="@dimen/activity_horizontal_margin"
        android:padding="16dp"
        android:text="@string/intro_screen"
        android:textStyle="bold"
        android:textColor="@color/colorAccent"/>
</RelativeLayout>

 

AdmobApplication.java

In the main project package, right click on the folder to display a menu. In the menu, select New > Java class and a dialog window will appear in the screen. Enter the name of the file as AdmobApplication and click ok.

Open this file in your IDE and paste the code below inside the file.

import android.app.Application;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class AdmobApplication extends Application{
    public InterstitialAd mInterstitialAd;
    public void createWallAd(){
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
    }
    public void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("bfbd3a3c054112a0")
                .build();
        mInterstitialAd.loadAd(adRequest);
    }
    public boolean isAdLoaded(){
        if (mInterstitialAd.isLoaded()) {
            return true;
        }
        return false;
    }
    public void displayLoadedAd(){
        mInterstitialAd.show();
    }
}

 

Build.Gradle

This class method and attributes are associated with the Admob. Right now you will see error in your development environment. The red error is because your IDE cannot find Google Admob files.

We have to add a dependency to our project in other to make all these classes and resources available to us.

Copy and paste this single line of code in the app build.gradle.

compile 'com.google.android.gms:play-services-ads:9.0.1'

Now, the complete code in the file will look like below.

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.inducesmile.androidadmob"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.google.android.gms:play-services-ads:9.0.1'
}

 

MainActivity.java

In the MainActivity class, we are going to add a Handle with posrDelay for 5 seconds to give our Admob Interstital ad to finish loading. Feel free to choose whatever value that works for you.

Open the file and add the below code to it

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
public class MainActivity extends AppCompatActivity {
    private final int SPLASH_DISPLAY_LENGTH = 5000;
    private AdmobApplication admobApp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        admobApp = (AdmobApplication)getApplication();
        admobApp.createWallAd();
        admobApp.requestNewInterstitial();
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run()
            {
                //Finish the splash activity so it can't be returned to.
                MainActivity.this.finish();
                if(admobApp.isAdLoaded()){
                    admobApp.displayLoadedAd();
                    admobApp.mInterstitialAd.setAdListener(new AdListener() {
                        @Override
                        public void onAdClosed() {
                            // Create an Intent that will start the main activity.
                            Intent mainIntent = new Intent(MainActivity.this, HomeScreenActivity.class);
                            MainActivity.this.startActivity(mainIntent);
                        }
                    });
                }else{
                    // Create an Intent that will start the main activity.
                    Intent mainIntent = new Intent(MainActivity.this, HomeScreenActivity.class);
                    MainActivity.this.startActivity(mainIntent);
                }
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

When the ad is closed by a user, it will move to the HomeScreenActivity class. The class layout also contains a single TextView that displays Welcome to Home Screen. The codes are as follows below.

 

activity_home_screen_xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.inducesmile.androidadmob.HomeScreenActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="@dimen/activity_horizontal_margin"
        android:padding="16dp"
        android:text="@string/welcome"
        android:textStyle="bold"
        android:textColor="@color/colorAccent"/>
</RelativeLayout>

 

HomeScreenActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class HomeScreenActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
    }
}

This brings us to the end of this tutorial. I hope that you have learn something. Run your app to see for yourself.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email address so that you will be among the first to receive my new android blog post once it is published.

Please if you love this tutorial, kindly download my android app – Complete Mathematics – in Google Play Store and let me know what you think about it.

OTHER INTERESTING POSTS:

2 Comments

Add a Comment