Skip to content

Commit 981f415

Browse files
malandr2copybara-github
authored andcommitted
Add AdListener events to BannerExample MainActivity
PiperOrigin-RevId: 868668555
1 parent bcefffe commit 981f415

File tree

1 file changed

+37
-0
lines changed
  • kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample

1 file changed

+37
-0
lines changed

kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import android.widget.Toast
2626
import androidx.appcompat.app.AppCompatActivity
2727
import androidx.core.view.ViewCompat
2828
import androidx.core.view.WindowInsetsCompat
29+
import com.google.android.gms.ads.AdListener
2930
import com.google.android.gms.ads.AdRequest
3031
import com.google.android.gms.ads.AdSize
3132
import com.google.android.gms.ads.AdView
33+
import com.google.android.gms.ads.LoadAdError
3234
import com.google.android.gms.ads.MobileAds
3335
import com.google.android.gms.ads.RequestConfiguration
3436
import com.google.android.gms.example.bannerexample.databinding.ActivityMainBinding
@@ -156,6 +158,41 @@ class MainActivity : AppCompatActivity() {
156158
binding.adViewContainer.addView(adView)
157159
// [END create_ad_view]
158160

161+
// Listen for ad events.
162+
adView.adListener =
163+
object : AdListener() {
164+
override fun onAdLoaded() {
165+
// Called when an ad is loaded.
166+
Log.d(TAG, "Ad loaded.")
167+
}
168+
169+
override fun onAdFailedToLoad(error: LoadAdError) {
170+
// Called when an ad request failed.
171+
Log.i(TAG, "Ad failed to load: ${error.message}")
172+
}
173+
174+
override fun onAdOpened() {
175+
// Called when an ad opens an overlay that covers the screen.
176+
Log.d(TAG, "Ad opened.")
177+
}
178+
179+
override fun onAdClicked() {
180+
// Called when a click is recorded for an ad.
181+
Log.d(TAG, "Ad clicked.")
182+
}
183+
184+
override fun onAdImpression() {
185+
// Called when an impression is recorded for an ad.
186+
Log.d(TAG, "Ad recorded an impression.")
187+
}
188+
189+
override fun onAdClosed() {
190+
// Called when the user is about to return to the application
191+
// after tapping on an ad.
192+
Log.d(TAG, "Ad closed.")
193+
}
194+
}
195+
159196
// [START load_ad]
160197
val adRequest = AdRequest.Builder().build()
161198
adView.loadAd(adRequest)

0 commit comments

Comments
 (0)