Reputation: 81
I am a beginner and I need help implementing Admob Interstitial Ad in the android studio using Kotlin. Sorry for my question, Appreciate it if you can show me step by step so I can learn the process and do it myself every other time. Thanks in advance.
package com.example.myapplication
import android.content.ContentValues.TAG
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.google.android.gms.ads.*
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
class MainActivity : AppCompatActivity() {
private var mInterstitialAd: InterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val one_Page: TextView = findViewById(R.id.one_Page)
val three_Page: TextView = findViewById(R.id.three_Page)
one_Page.setOnClickListener {
val intent = Intent(this, PageActivity2::class.java)
// start your next activity
startActivity(intent)
}
three_Page.setOnClickListener {
val intent = Intent(this, PageActivity2::class.java)
// start your next activity
startActivity(intent)
}
}
}
Upvotes: 4
Views: 2608
Reputation: 4791
I think maybe you are missing a few steps to get it right, to do that please follow these steps:
Note: I will use sample ids to load demo ads, you need to make sure it's worked then think about replacing the sample ids with real ids
1: Setup Admob dependencies:
implementation 'com.google.android.gms:play-services-ads:20.1.0'
2: Add snippet code to your AndroidManifest.xml inside the <application>
tag
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<!-- Replace sample id if you have a real id from AdMob project - pattern ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
3: Initialize Admob and load an ad:
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
class MainActivity : AppCompatActivity() {
private var mInterstitialAd: InterstitialAd? = null
private final var TAG = 'MainActivity'
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize Admob
MobileAds.initialize(this) {}
var adRequest = AdRequest.Builder().build()
// ca-app-pub-3940256099942544/1033173712 is a sample interstial id.
InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.message)
mInterstitialAd = null
}
override fun onAdLoaded(interstitialAd: InterstitialAd) {
Log.d(TAG, 'Ad was loaded.')
mInterstitialAd = interstitialAd
}
})
}
}
4: Add the FullScreenContentCallback below InterstitialAd.load
:
mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
Log.d(TAG, 'Ad was dismissed.')
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
Log.d(TAG, 'Ad failed to show.')
}
override fun onAdShowedFullScreenContent() {
Log.d(TAG, 'Ad showed fullscreen content.')
mInterstitialAd = null;
}
}
6: Add showFullAd to MainActivity and call showFullAd
in the activity where you want to show it
fun showFullAd(){
if (mInterstitialAd != null) {
mInterstitialAd?.show(this)
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.")
}
}
See more:
Upvotes: 3
Reputation: 8931
https://developers.google.com/admob/android/interstitial#kotlin explains how to request interstitial ads. Your code above created a mInterstitialAd
variable but needs to still:
Upvotes: 0