To monetize the android apps, Google provided a mobile ad network called AdMob to show the ads in applications. If we want to earn the revenue from our android apps, then AdMob platform is the perfect solution to easily integrate the ads in our android apps.
Different Types of Ad Formats
AdMob network provides a different types of ad formats, so we can choose the ads which best fits to our app’s user experience. Following are the different types of ad formats available for android apps.
- Banner Ads
- Interstitial Ads
- Rewarded Video Ads
Here we will learn about AdMob Interstitial Ads, in next chapters we will learn about other AdMob ads.
Interstitial Ads are full-screen ads that cover the interface of an app until closed by the user. They're best used at natural pauses in the flow of an app's execution, such as in between levels of a game or just after completing a task.
To earn the revenue by integrating Google AdMob ads in android applications, we need to follow below steps.
- First we need to register an account in AdMob
- Create ad units in AdMob
- Integrate Google Mobile Ads SDK into an app,
To perform above steps, check this Android Integrate Google AdMob SDK Tutorial.
Create & Load an Interstitial Ad in App
By using InterstitialAd object, we can easily integrate and show the interstitial ads in android applications.
Following is the example of creating and showing the interstitial ads in android application using InterstitialAd object.
private InterstitialAd interstitialAd;@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pub-4761500786576152~8215465788");
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest request = new AdRequest.Builder().build();
interstitialAd.loadAd(request);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pub-4761500786576152~8215465788");
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest request = new AdRequest.Builder().build();
interstitialAd.loadAd(request);
}
Show the Interstitial Ad
Generally, the AdMob Interstitial ads should be displayed during the natural pauses in the flow of an app. For example, between changing the levels of a game or after completing the task based on our requirements.
To show an interstitial ads, first we need to check whether the ad loaded or not using isLoaded() method, then we need to call show() method to show the an interstitial ad like as shown below.
interstitialAd.setOnClickListener(new View.OnClickListener(){
@Override public void onClick(View v){
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
If you observe above code, we are showing our ad on button click by checking whether the ad loaded or not based on our requirements.
Now we will see how to integrate AdMob Ad Units in our android application to show the interstitial ads with example.
Android Integrate AdMob Interstitial Ads Example
Following is the example of integrating AdMob interstitial ads in android application to generate the revenue by displaying the ads.
Create a new android application using android studio and give names as AdMobExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
To start integrating AdMob ads in our android application, we need to import the Google Mobile Ads SDK with Gradle dependency that points to Google’s Maven repository, for that open your app à Gradle Scripts à build.gradle (project) and add maven repository in allprojects section like as shown below.
allprojects {
repositories {
jcenter()
maven{
url "https://maven.google.com"
}
}
}
repositories {
jcenter()
maven{
url "https://maven.google.com"
}
}
}
Now we need to add google play services to our application, for that open your app à Gradle Scripts à build.gradle (Module: app) and add following google play services compile statement to the dependencies{} section.
dependencies {
...
compile 'com.google.android.gms:play-services-ads:11.8.0'
...
compile 'com.google.android.gms:play-services-ads:11.8.0'
...
}
}
The above one instructs Gradle to pull the latest version of the Mobile Ads SDK. Once you are done with adding required dependencies, save the file and perform a Gradle sync.
Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.
activity_main.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_height="match_parent"android:layout_width="match_parent"tools:context="com.tutlane.admobexample.MainActivity">
<TextView android:text="Welcome to Tutlane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="150dp"/>
</RelativeLayout>
<TextView android:text="Welcome to Tutlane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="150dp"/>
</RelativeLayout>
Now open your main activity file MainActivity.java from \java\com.tutlane.admobexample path and write the code like as shown below
MainActivity.java
package com.tutlane.admobexample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import com.google.android.gms.ads.AdListener;import com.google.android.gms.ads.AdRequest;import com.google.android.gms.ads.InterstitialAd;import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pub-4761500786576152~8215465788");
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest request = new AdRequest.Builder().build();
interstitialAd.loadAd(request);
interstitialAd.setAdListener(new AdListener(){
public void onAdLoaded(){
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
}
}
public class MainActivity extends AppCompatActivity {
private InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pub-4761500786576152~8215465788");
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest request = new AdRequest.Builder().build();
interstitialAd.loadAd(request);
interstitialAd.setAdListener(new AdListener(){
public void onAdLoaded(){
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
}
}
If you observe above code, we used loadAd() method of InterstitialAd class to load an ad with AdRequest input parameter. Here AdRequest parameter is required to hold the runtime information about a single ad request.
Output of Android AdMob Interstitial Ads Example
When we run above example in android emulator we will get a result like as shown below.
As discussed Interstitial Ads are the full screen ads and those ads will cover complete interface of our app. If you observe above result, our app interface completely covered with full screen interstitial ad.
This is how we can integrate Google AdMob Interstitial ads in our android applications to generate the revenue based on our requirements.
No comments:
Post a Comment