@@ -26,9 +26,11 @@ import android.widget.Toast
2626import androidx.appcompat.app.AppCompatActivity
2727import androidx.core.view.ViewCompat
2828import androidx.core.view.WindowInsetsCompat
29+ import com.google.android.gms.ads.AdListener
2930import com.google.android.gms.ads.AdRequest
3031import com.google.android.gms.ads.AdSize
3132import com.google.android.gms.ads.AdView
33+ import com.google.android.gms.ads.LoadAdError
3234import com.google.android.gms.ads.MobileAds
3335import com.google.android.gms.ads.RequestConfiguration
3436import 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