How add Interstitial Ads in Android.

This example guide how to integrate interstitial ads into an Android app. Interstitial ads are Full Page Ads. If you not create your own app and Interstitial ad unit in the AdMob UI and use your newly created app ID and ad unit ID in your code. Remember to configure your device with test ads. More Learn about ad targeting and Interstitial ad guidance. About integrating Google AdMob in apps and start earning.

Create New Project

1. First create a new project in Android Studio (File => New Project)

build.gradle(Module: app)

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

AndroidManifest.xml


<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	
	<!--   Add this in below code inside application  part-->
	
	<activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
	
	

string.xml

									   
  <resources>
    <string name="app_name">BannerAdsShow</string>
 <!-- 
For Demo use below Id (  Don't use Original Id to Test ads in you app . For Demo use Replace last three number of you adUnit Id  to  111 for 
Test ads in your device  )
 -->
   
    <string name="banner_demo">ca-app-pub-1808690293181798/2213366111</string>

</resources>

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"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Full Screen Ads Show "
        android:textAppearance="?android:attr/textAppearanceMedium"/>

</RelativeLayout>

Mainactivity.java

									   
package com.narendra.interstitialads;

import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {

    Context context;
    InterstitialAd interstitialAd;

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

        interstitialAd = new InterstitialAd(context);
        interstitialAd.setAdUnitId(getString(R.string.inti_ads));

        Log.w("-----------"," Device ID : "+getId(context));

        final AdRequest adRequest = new AdRequest.Builder().addTestDevice(getId(context)).build();

        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();

                interstitialAd.show();
            }
        });
        interstitialAd.loadAd(adRequest);
    }

    @Override
    protected void onResume() {
        super.onResume();
        showInterstitial();

    }

    @Override
    protected void onPause() {
        super.onPause();
        showInterstitial();
    }

    private void showInterstitial() {
        // Show the ad if it's ready. Otherwise toast and restart the game.
        if (interstitialAd != null && interstitialAd.isLoaded()) {
            interstitialAd.show();
        }
    }

    public static String getId(Context context) {
        String deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(deviceId.getBytes());
            byte messageDigest[] = digest.digest();

            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {

                String h = Integer.toHexString(0xFF & messageDigest[i]);

                while (h.length() < 2)
                    h = "0" + h;

                hexString.append(h);
            }
            deviceId = hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            deviceId = "";
        } finally {
            return deviceId.toUpperCase();
        }
    }
}

Output :

Tags

  • Android

  • Android Studio

  • Android development

  • Java