1. Are you ready for the Galaxy S20? Here is everything we know so far!

InterstitialAd's broken Android Studio 4.1.1

Discussion in 'Android Development' started by Kaplah, Apr 16, 2021.

  1. Kaplah

    Kaplah Lurker
    Thread Starter

    New-ish android dev, 15 year experience C, C++, Java, script language dev.

    Working with Android Studio 4.1.1 and trying to work out a basic scenario. I've built my main activity with just a button that feeds to a second activity that's a google admob interstitial ad.

    The admob activity is just out of the box. I've not modified it from the template other than to give it a name.

    Out the door it's using what I understand to be the old library:

    import com.google.android.gms.ads.InterstitialAd

    Which no longer exists. It should be...

    import com.google.android.gms.ads.interstitial.InterstitialAd

    Once I compile I get 8 errors. I'm assuming updates to the admob framework make this old template obsolete. I'm not sure what needs to be done to correct this. Is there an updated template I can snag, something that's communal knowledge, or does someone know what's missing from the outdated OOTB template that will get me working?

    Here's the code, but I shouldn't need it as we're talking OOTB stuff.

    Code (Text):
    1. package com.example.testapp
    2.  
    3. import com.google.android.gms.ads.AdListener
    4. import com.google.android.gms.ads.AdRequest
    5. import com.google.android.gms.ads.interstitial.InterstitialAd
    6. import android.widget.Button
    7. import android.widget.TextView
    8.  
    9. import android.os.Bundle
    10. import androidx.appcompat.app.AppCompatActivity
    11. import android.view.Menu
    12. import android.view.MenuItem
    13. import android.widget.Toast
    14.  
    15. // Remove the line below after defining your own ad unit ID.
    16. private const val TOAST_TEXT = "Test ads are being shown. " +
    17.         "To show live ads, replace the ad unit ID in res/values/strings.xml " +
    18.         "with your own ad unit ID."
    19. private const val START_LEVEL = 1
    20.  
    21. class ad1PostButtonPush : AppCompatActivity() {
    22.  
    23.     private var currentLevel: Int = 0
    24.     private var interstitialAd: InterstitialAd? = null
    25.     private lateinit var nextLevelButton: Button
    26.     private lateinit var levelTextView: TextView
    27.  
    28.     override fun onCreate(savedInstanceState: Bundle?) {
    29.         super.onCreate(savedInstanceState)
    30.         setContentView(R.layout.activity_ad1_post_button_push)
    31.  
    32.         // Create the next level button, which tries to show an interstitial when clicked.
    33.         nextLevelButton = findViewById(R.id.next_level_button)
    34.         nextLevelButton.isEnabled = false
    35.         nextLevelButton.setOnClickListener { showInterstitial() }
    36.  
    37.         levelTextView = findViewById(R.id.level)
    38.         // Create the text view to show the level number.
    39.         currentLevel = START_LEVEL
    40.  
    41.         // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml).
    42.         interstitialAd = newInterstitialAd()
    43.         loadInterstitial()
    44.  
    45.         // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
    46.         Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show()
    47.     }
    48.  
    49.     override fun onCreateOptionsMenu(menu: Menu): Boolean {
    50.         // Inflate the menu; this adds items to the action bar if it is present.
    51.         menuInflater.inflate(R.menu.menu_ad1_post_button_push, menu)
    52.         return true
    53.     }
    54.  
    55.     override fun onOptionsItemSelected(item: MenuItem) =
    56.             when (item.itemId) {
    57.                 R.id.action_settings -> true
    58.                 else -> super.onOptionsItemSelected(item)
    59.             }
    60.  
    61.     private fun newInterstitialAd(): InterstitialAd {
    62.         return InterstitialAd().apply {
    63.             adUnitId = getString(R.string.interstitial_ad_unit_id)
    64.             adListener = object : AdListener() {
    65.                 override fun onAdLoaded() {
    66.                     nextLevelButton.isEnabled = true
    67.                 }
    68.  
    69.                 override fun onAdFailedToLoad(errorCode: Int) {
    70.                     nextLevelButton.isEnabled = true
    71.                 }
    72.  
    73.                 override fun onAdClosed() {
    74.                     // Proceed to the next level.
    75.                     goToNextLevel()
    76.                 }
    77.             }
    78.         }
    79.     }
    80.  
    81.     private fun showInterstitial() {
    82.         // Show the ad if it"s ready. Otherwise toast and reload the ad.
    83.         if (interstitialAd?.isLoaded == true) {
    84.             interstitialAd?.show()
    85.         } else {
    86.             Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show()
    87.             goToNextLevel()
    88.         }
    89.     }
    90.  
    91.     private fun loadInterstitial() {
    92.         // Disable the next level button and load the ad.
    93.         nextLevelButton.isEnabled = false
    94.         val adRequest = AdRequest.Builder()
    95.                 .setRequestAgent("android_studio:ad_template")
    96.                 .build()
    97.         interstitialAd?.loadAd(adRequest)
    98.     }
    99.  
    100.     private fun goToNextLevel() {
    101.         // Show the next level and reload the ad to prepare for the level after.
    102.         levelTextView.text = "Level " + (++currentLevel)
    103.         interstitialAd = newInterstitialAd()
    104.         loadInterstitial()
    105.     }
    106. }
    Here are the errors

     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...