Android How to Integrate Google AdMob in your App

AdMob is a multi-platform mobile ad network that allows you to monetize your Android app. By integrating AdMob you can start earning right away. It is very useful particularly when you are publishing a free app and want to earn some money from it Integrating AdMob is such an easy task that it takes no more than 5mins. In this article, we’ll build a simple app with two screens to show the different types of ads that AdMob supports.

1. Type of AdMob Ads

Banner Ad
Banner Ads occupies the only portion of the screen depending on the ad size that is created. It comes in multiple sizes Standard, Medium, Large, Full-Size, Leaderboard, and Smart Banner. Smart banners are very useful when you target multiple device sizes and fit the same ad depending on the screen size.

 Interstitial Ad
Interstitial ads occupy a full screen of the app. Basically, they will be shown on a timely basis, between screen transition or when the user is done with a task. Usually, we can see these ads in games displaying Ad when a level is completed.

Rewarded Video Ad
Rewarded Video Ads are fullscreen video ads which offer some reward points if the user watches the ad video. These ads are very useful to offer some reward points/coins in video games.

 Native Ad
Native Ads offers flexibility to configure the ad appearance like color, text color and buttons to make them appear as a native component of the app.

2. Creating Ad Units

Note: AdMob admin interface changes quite often. The below steps to create Ad Unit IDs might differ from time to time.

1. Sign into your AdMob

2. Create a new App by giving the package name of the app you want to integrate AdMob. Once the App is created, you can find the APP ID on the dashboard which looks like ca-app-pub-XXXXXXXXX~XXXXXXXXX.

3. Select the newly created App and click on ADD AD UNITbutton to create a new ad unit.

4. Select the ad format and give the ad unit a name.

5. Once the ad unit is created, you can notice the Ad unit on the dashboard. An example of ad unit id look like ca-app-pub-0XXXXXXXXX/XXXXXXXXXXX

 

Open Your Admob Account and click on Apps And your Apps

 

 

 

 

 

 

 

 

 

 

Select on your app is publicsh or not in google play store or not if publice your app serch your app

 

 

 

 

 

Create Ads Type like Banner, Interstitial Rewarded  Ads.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Step 1: First one to  Start Android Studio

Step 2 :  Seconds step to Create a New Project Project ClickOn  ==> File  ==> NEW ==> New Project

Step 3: Gradle Scripts Open Project base and add these

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:28+'
    compile 'com.android.support:design:28+'
    compile 'com.android.support:cardview-v7:28+'
    compile 'com.google.android.gms:play-services-ads:17.0.0'

 

4. Add the App ID and Ad unit IDs to your strings.xml. Open strings.xml located under res ⇒ values and add the IDs of all the ad units.

 
<resources>
<string name="app_name">AdMob</string>
<string name="activity_second_activiy">Interstitial</string>
<string name="msg_welcome">Welcome to Admob. Click on the below button to launch the Interstitial ad.</string>
<string name="btn_fullscreen_ad"> Interstitial Ad</string>
<string name="btn_rewarded_video">Rewarded Video Ad</string>

<!-- TODO - add your ad unit Ids -->
<!-- AdMob ad unit IDs -->
<string name="admob_app_id">ca-app-pub-XXXXXXXX~XXXXXXXXXXX</string>
<string name="banner_home_footer">ca-app-pub-XXXXXXXX~XXXXXXXXXXX</string>
<string name="interstitial_full_screen">ca-app-pub-XXXXXXXX~XXXXXXXXXXX</string>
<string name="rewarded_video">ca-app-pub-XXXXXXXX~XXXXXXXXXXX</string>
</resources>

4. Create a class named MyApplication.java and extend the class from Application. In this application class, we have to globally initialize the AdMob App Id. Here we use MobileAds.initialize() method to initialize the AdMob.

3.1 Adding Banner Ad

Banner ads occupies only a portion of the screen. I am adding a banner ad in my main activity aligning to bottom of the screen. In order to add the banner ad, you need to add com.google.android.gms.ads.AdViewelement to your xml layout.

 

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_home_footer">
</com.google.android.gms.ads.AdView>

6. Open the layout file of your main activity (activity_main.xml) and add the AdView widget. I am also adding a button to launch another in which we’ll try Interstitial ad.

 

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="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="com.codeplayon.MainActivity">

<Button
android:id="@+id/btn_fullscreen_ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/btn_fullscreen_ad" />

<Button
android:id="@+id/btn_show_rewarded_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_fullscreen_ad"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/btn_rewarded_video" />

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_home_footer">
</com.google.android.gms.ads.AdView>
</RelativeLayout>

7. Open MainActivity.java and modify the code as shown.

MainActivity.java
package com.codeplayon.admob;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private Button btnFullscreenAd, btnShowRewardedVideoAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnFullscreenAd = (Button) findViewById(R.id.btn_fullscreen_ad);
btnShowRewardedVideoAd = (Button) findViewById(R.id.btn_show_rewarded_video);
btnFullscreenAd.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, InterstitialAdActivity.class));
}
});

btnShowRewardedVideoAd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, RewardedVideoAdActivity.class));
}
});
// TODO - remove this if condition
// it's for demo purpose
if (TextUtils.isEmpty(getString(R.string.banner_home_footer))) {
Toast.makeText(getApplicationContext(), "Please mention your Banner Ad ID in strings.xml", Toast.LENGTH_LONG).show();
return;
}
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(getString(R.string.banner_home_footer));
MobileAds.initialize(this, "ca-app-pub-XXXXXXXXXXXXXXXXXXXX");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// Check the LogCat to get your test device ID
.addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXX")
.build();
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
}

@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
}

@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}

@Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}

@Override
public void onAdOpened() {
super.onAdOpened();
}
});
mAdView.loadAd(adRequest);
}

@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}

@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}

@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}}

 

3.2 Adding Interstitial Ad (Fullscreen Ad)

8. Create an activity named SecondActivity.java by right-clicking on package New ⇒ Activity ⇒ Empty Activity.

 

SecondActivity.java
package com.codeplayon.admob;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class SecondActivity extends AppCompatActivity {
private String TAG = SecondActivity.class.getSimpleName();

InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mInterstitialAd = new InterstitialAd(this);
// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder()
.build();
// Load ads into Interstitial Ads
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {

public void onAdLoaded() {
showInterstitial();
}
});
}

private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
}

3.3 Adding Rewarded Video Ad

9. Create another activity named RewardedVideoAdActivity.java and add the below code. This is same as Interstitial but there will be a callback method onRewarded() called when there is a reward after watching the video ad completely.

 

RewardedVideoAdActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;


public class RewardedVideoAdActivity extends AppCompatActivity {
private RewardedVideoAd mRewardedVideoAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rewarded_video_ad);
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(RewardedVideoAdActivity.this, "onRewarded! currency: " + rewardItem.getType() + "  amount: " +
rewardItem.getAmount(), Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLeftApplication",
Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoStarted() {
Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}
});
loadRewardedVideoAd();
}

private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd(getString(R.string.rewarded_video),
new AdRequest.Builder().build());
// showing the ad to user
showRewardedVideo();
}

private void showRewardedVideo() {
// make sure the ad is loaded completely before showing it
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();}
}

@Override
public void onResume() {
mRewardedVideoAd.resume(this);
super.onResume();
}

@Override
public void onPause() {
mRewardedVideoAd.pause(this);
super.onPause();
}

@Override
public void onDestroy() {
mRewardedVideoAd.destroy(this);
super.onDestroy();
}}

You may also like...