Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
- Kotlin Tutorial
Practical Paper
Industrial Training
Google AdMob Interstitial Ads
In this tutorial, we implement the Google AdMob Interstitial Ads in our Android application. To place the Google AdMob in Android application, we need to create the Google Ad Unit Id. The complete reference for creating the Google AdMod account and generate the Ad Unit Id is described at Android Google AdMob.
An Interstitials ad is the full-screen ad that covers the entire activity layout. This ad is displayed at a transition point of an activity. To implement the Google AdMob in the Android application, choose Google AdMob Ads Activity and select the Ad format type as Interstitials.
We are also able to place the Google AdMob Ads on other activities such as Blank Activity.
Add the Google ads dependency 'com.google.android.gms:play-services-ads:17.0.0' in the build.gradle file:
build.gradle
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.google.android.gms:play-services-ads:17.0.0' testImplementation 'junit:junit:4.12' } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.google.android.gms:play-services-ads:17.0.0' testImplementation 'junit:junit:4.12' }
activity_main.xml
Add your UI code in the activity_main.xml. The Button component is used to load the ad.
< 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" tools:context="example.javatpoint.com.kotlininterstitialads.MainActivity"> < !-- view for AdMob Interstitial Ad --> < TextView android:id="@+id/app_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="@string/interstitial_ad_sample" android:textAppearance="?android:attr/textAppearanceLarge" /> < Button android:id="@+id/load_ad_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/load_ad" /> < /RelativeLayout> < 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" tools:context="example.javatpoint.com.kotlininterstitialads.MainActivity"> < !-- view for AdMob Interstitial Ad --> < TextView android:id="@+id/app_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="@string/interstitial_ad_sample" android:textAppearance="?android:attr/textAppearanceLarge" /> < Button android:id="@+id/load_ad_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/load_ad" /> < /RelativeLayout>
strings.xml
Add the created ad unit id in the string.xml file.
< resources> < string name="app_name">KotlinInterstitialAds< /string> < string name="action_settings">Settings< /string> < string name="interstitial_ad_sample">Interstitial Ad Sample< /string> < string name="load_ad">Load Ad< /string> < !-- - This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id. --> < string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712< /string> < /resources> < resources> < string name="app_name">KotlinInterstitialAds< /string> < string name="action_settings">Settings< /string> < string name="interstitial_ad_sample">Interstitial Ad Sample< /string> < string name="load_ad">Load Ad< /string> < !-- - This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id. --> < string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712< /string> < /resources>
MainActivity.kt
Add the following code in MainActivity.kt class. To load the ad on the UI, create the instance of InterstitialAd and initialize the ad unit id on InterstitialAd interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id). Override the InterstitialAd listeners onAdLoaded(), onAdFailedToLoad(), onAdClosed. To load the ad on clicking the button, create the instance of AdRequest and load the ad by calling InterstitialAd!!.loadAd(AdRequest).
package example.javatpoint.com.kotlininterstitialads import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.InterstitialAd import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() { private var mLoadAdButton: Button? = null private var mInterstitialAd: InterstitialAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml). mInterstitialAd = newInterstitialAd() loadInterstitial() // Create the load ad button, tries to show an interstitial when clicked. mLoadAdButton = findViewById(R.id.load_ad_button) as Button mLoadAdButton!!.isEnabled = false mLoadAdButton!!.setOnClickListener { showInterstitial() } } private fun newInterstitialAd(): InterstitialAd { val interstitialAd = InterstitialAd(this) interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id) interstitialAd.adListener = object : AdListener() { override fun onAdLoaded() { mLoadAdButton!!.isEnabled = true Toast.makeText(applicationContext, "Ad Loaded", Toast.LENGTH_SHORT).show() } override fun onAdFailedToLoad(errorCode: Int) { mLoadAdButton!!.isEnabled = true Toast.makeText(applicationContext, "Ad Failed To Load", Toast.LENGTH_SHORT).show() } override fun onAdClosed() { // Proceed to the next level. // goToNextLevel() Toast.makeText(applicationContext, "Ad Closed", Toast.LENGTH_SHORT).show() tryToLoadAdOnceAgain() } } return interstitialAd } private fun loadInterstitial() { // Disable the load ad button and load the ad. mLoadAdButton!!.isEnabled = false val adRequest = AdRequest.Builder().build() mInterstitialAd!!.loadAd(adRequest) } private fun showInterstitial() { // Show the ad if it is ready. Otherwise toast and reload the ad. if (mInterstitialAd != null && mInterstitialAd!!.isLoaded) { mInterstitialAd!!.show() } else { Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show() tryToLoadAdOnceAgain() } } private fun tryToLoadAdOnceAgain() { mInterstitialAd = newInterstitialAd() loadInterstitial() } } package example.javatpoint.com.kotlininterstitialads import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.InterstitialAd import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() { private var mLoadAdButton: Button? = null private var mInterstitialAd: InterstitialAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml). mInterstitialAd = newInterstitialAd() loadInterstitial() // Create the load ad button, tries to show an interstitial when clicked. mLoadAdButton = findViewById(R.id.load_ad_button) as Button mLoadAdButton!!.isEnabled = false mLoadAdButton!!.setOnClickListener { showInterstitial() } } private fun newInterstitialAd(): InterstitialAd { val interstitialAd = InterstitialAd(this) interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id) interstitialAd.adListener = object : AdListener() { override fun onAdLoaded() { mLoadAdButton!!.isEnabled = true Toast.makeText(applicationContext, "Ad Loaded", Toast.LENGTH_SHORT).show() } override fun onAdFailedToLoad(errorCode: Int) { mLoadAdButton!!.isEnabled = true Toast.makeText(applicationContext, "Ad Failed To Load", Toast.LENGTH_SHORT).show() } override fun onAdClosed() { // Proceed to the next level. // goToNextLevel() Toast.makeText(applicationContext, "Ad Closed", Toast.LENGTH_SHORT).show() tryToLoadAdOnceAgain() } } return interstitialAd } private fun loadInterstitial() { // Disable the load ad button and load the ad. mLoadAdButton!!.isEnabled = false val adRequest = AdRequest.Builder().build() mInterstitialAd!!.loadAd(adRequest) } private fun showInterstitial() { // Show the ad if it is ready. Otherwise toast and reload the ad. if (mInterstitialAd != null && mInterstitialAd!!.isLoaded) { mInterstitialAd!!.show() } else { Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show() tryToLoadAdOnceAgain() } } private fun tryToLoadAdOnceAgain() { mInterstitialAd = newInterstitialAd() loadInterstitial() } }
AndroidManifest.xml
Add the following code in AndroidManifest.xml file:
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.javatpoint.com.kotlininterstitialads"> < !-- Include required permissions for Google Mobile Ads to run. --> < uses-permission android:name="android.permission.INTERNET" /> < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> < !-- This meta-data tag is required to use Google Play Services. --> < meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> < activity android:name=".MainActivity" android:label="@string/app_name"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < !-- Include the AdActivity configChanges and theme. --> < activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> < meta-data android:name="com.google.android.gms.ads.AD_MANAGER_APP" android:value="true"/> < /application> < /manifest> < ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.javatpoint.com.kotlininterstitialads"> < !-- Include required permissions for Google Mobile Ads to run. --> < uses-permission android:name="android.permission.INTERNET" /> < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> < !-- This meta-data tag is required to use Google Play Services. --> < meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> < activity android:name=".MainActivity" android:label="@string/app_name"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> < meta-data android:name="com.google.android.gms.ads.AD_MANAGER_APP" android:value="true"/> < /application> < /manifest>
Output: