Android Admob not showing interstitial ad with test app ID in kotlin

3 days ago 7
ARTICLE AD BOX

This is my SettingActivity file in which i m trying to display the ad via an ad buttton but everytime i try to run it will show in logcat that interstitial ad is not loaded, what could be the probable issue and how can I resolve it

import android.content.Intent import android.net.Uri import android.os.Build import android.os.Bundle import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import com.example.cointoss.databinding.ActivitySettingBinding import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.FullScreenContentCallback import com.google.android.gms.ads.LoadAdError import com.google.android.gms.ads.MobileAds import com.google.android.gms.ads.interstitial.InterstitialAd import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback class SettingActivity : AppCompatActivity() { private lateinit var binding: ActivitySettingBinding private var interstitialAd: InterstitialAd? = null override fun onCreate(savedInstanceState: Bundle?) { MobileAds.initialize(this) {} if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.statusBarColor = ContextCompat.getColor(this, R.color.lightgrey) } binding= ActivitySettingBinding.inflate(layoutInflater) super.onCreate(savedInstanceState) setContentView(binding.root) loadAd() // loadInterstitial() binding.shareTv.setOnClickListener { val intent= Intent().apply{ type = "text/plain" action = Intent.ACTION_SEND } startActivity(intent) } binding.feedbackTv.setOnClickListener { val recipient = "[email protected]" val subject = "App Feedback" val body = "I want to share my thoughts on your app..." val uriString = String.format("mailto:%s?subject=%s&body=%s", Uri.encode(recipient), Uri.encode(subject), Uri.encode(body) ) val uri = Uri.parse(uriString) val emailIntent = Intent(Intent.ACTION_SENDTO, uri).apply { setPackage("com.google.android.gm") } startActivity(emailIntent) } binding.rateTv.setOnClickListener { RateAppDialog.show(this) } binding.adbtn.setOnClickListener { // showAd() if (interstitialAd != null) { interstitialAd?.show(this) } else { showAd() // fallback if ad is not ready } } } // private fun loadInterstitial() { // val adRequest = AdRequest.Builder().build() // InterstitialAd.load( // this, // "ca-app-pub-3940256099942544/1033173712", // replace with real ad unit ID // adRequest, // object : InterstitialAdLoadCallback() { // override fun onAdLoaded(ad: InterstitialAd) { // interstitialAd = ad // setAdCallbacks() // } // // override fun onAdFailedToLoad(error: LoadAdError) { // interstitialAd = null // } // } // ) // } // // private fun setAdCallbacks() { // interstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() { // override fun onAdDismissedFullScreenContent() { // interstitialAd = null // loadInterstitial() // preload next ad // doAction() // } // } // } // // private fun doAction() { // Toast.makeText(this, "Button action executed", Toast.LENGTH_SHORT).show() // // your actual button logic here // } private fun showAd() { if (interstitialAd != null) { interstitialAd?.show(this) Log.d("TAG", "Ad displayed. Loading next ad.") // After showing an ad, it can't be reused. Load a new one immediately. loadAd() } else { Log.d("TAG", "Interstitial ad is not ready yet.") // Optional: You can try to load an ad again if it's not ready // loadAd() } } private fun loadAd(){ val adRequest = AdRequest.Builder().build() InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() { override fun onAdFailedToLoad(adError: LoadAdError) { interstitialAd = null } override fun onAdLoaded(ad: InterstitialAd) { interstitialAd = ad } }) } }

here is my Manifest File that has all the tags which are required but still the ad is not showing what could be the issue

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.CoinToss" android:usesCleartextTraffic="true" tools:targetApi="31"> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> <activity android:name=".SettingActivity" android:exported="false" android:theme="@style/Theme.CoinToss" /> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:exported="false" android:theme="@android:style/Theme.Translucent"/> <meta-data android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION" android:value="true"/> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>``` the below are my dependency for app level build.gradle file ``` implementation("androidx.datastore:datastore-preferences:1.2.0") implementation(platform("com.google.firebase:firebase-bom:33.1.0")) implementation("com.google.firebase:firebase-config-ktx") implementation("com.google.firebase:firebase-analytics:21.6.2") implementation("com.google.android.gms:play-services-ads:24.8.0")
Read Entire Article