AdMob Interstitial Ads of andorid

Interstitial advertising are full-screen advertisements that cover the whole app's user interface. This tutorial will show you how to incorporate AdMob Interstitial advertisements into an Android app.

In this post, we will look at the process of adding a admob to an application by creating an app  of Mobile Application.

Example 

                                                                                                                                           

Dependencies of admob

  implementation(group = "com.google.android.gms", name = "play-services-ads", version = "19.3.0")
Step-by-Step Implementation 

Step 1: Create a New Project in Android Studio (File >new  project). 


Step 2: Create an MainActivity.kt file in your package folder . 

Implement the same invoke the fol​lowing code inside MainActivity.kt file.

 import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
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


class MainActivity : AppCompatActivity() {
lateinit var interstitialAd: InterstitialAd
@SuppressLint("VisibleForTests")
lateinit var adRequest: AdRequest
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

MobileAds.initialize(this)
adRequest = AdRequest.Builder().build()
interstitialAd = InterstitialAd(this)
interstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
interstitialAd.loadAd(adRequest)
interstitialAd.setAdListener(object : AdListener() {
override fun onAdLoaded() {
displayInterstitialAd(interstitialAd)
}
})

}
private fun displayInterstitialAd(interstitialAd: InterstitialAd) {
if (interstitialAd.isLoaded) {
interstitialAd.show()
}
}
}
Step 3 : Output of above example

                                          

Happy coding!

365Bloggy July 15, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive