Monday, October 7, 2019

Android Integrate AdMob Rewarded Video Ads in App

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
In previous chapters we learned about Banner Ads and Interstitial Ads, now we will learn about AdMob Rewarded Video Ads with examples.

Rewarded Video Ads are the full screen video ads that users have the option of watching in full in exchange for in-app rewards.

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 Rewarded Video Ads in App

By using RewardedVideoAd class, we can easily integrate and show the rewarded video ads in android applications.

Following is the example of creating and showing the rewarded video ads in android application using RewardedVideoAd object.

public class MainActivity extends AppCompatActivity {
    
private RewardedVideoAd rewardedVideoAd;
    
@Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        MobileAds.initialize(
this"ca-app-pub-4761500786576152~8215465788");
        
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        
rewardedVideoAd.setRewardedVideoAdListener(this);
        loadRewardedVideoAd();
    }
    
private void loadRewardedVideoAd(){
        AdRequest request = 
new AdRequest.Builder().build();
        
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", request);
        
if (rewardedVideoAd.isLoaded()) {
            
rewardedVideoAd.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 rewarded video ads with example.

Android Integrate AdMob Rewarded Ads Example

Following is the example of integrating AdMob rewarded 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"
        
}
    }
}
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'
    ...
}
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>
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.AdRequest;import com.google.android.gms.ads.MobileAds;import com.google.android.gms.ads.reward.RewardedVideoAd;
public class MainActivity extends AppCompatActivity {
    
private RewardedVideoAd rewardedVideoAd;
    
@Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        MobileAds.initialize(
this"ca-app-pub-4761500786576152~8215465788");
        
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        
rewardedVideoAd.setRewardedVideoAdListener(this);
        loadRewardedVideoAd();
    }
    
private void loadRewardedVideoAd(){
        AdRequest request = 
new AdRequest.Builder().build();
        
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", request);
        
if (rewardedVideoAd.isLoaded()) {
            
rewardedVideoAd.show();
        }
    }
}
If you observe above code, we used loadAd() method of RewardedVideoAd class to load an ad with AdRequest input parameter. Here AdRequest parameter is required to hold the runtime information about a single ad request.

When we run above example in android emulator, the Rewarded Video Ads will be displayed on the interface of our app.

This is how we can integrate Google AdMob Rewarded Video ads in our android applications to generate the revenue based on our requirements. 

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...