Skip to content

Commit c43e6aa

Browse files
malandr2copybara-github
authored andcommitted
Added Banner Snippets
PiperOrigin-RevId: 788154550
1 parent aaf6700 commit c43e6aa

File tree

2 files changed

+249
-0
lines changed
  • java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets
  • kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets

2 files changed

+249
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.android.gms.snippets;
16+
17+
import android.view.View;
18+
import android.view.ViewGroup;
19+
import androidx.annotation.NonNull;
20+
import com.google.android.gms.ads.AdListener;
21+
import com.google.android.gms.ads.AdView;
22+
import com.google.android.gms.ads.LoadAdError;
23+
import com.google.android.gms.ads.admanager.AdManagerAdView;
24+
import com.google.android.gms.ads.admanager.AppEventListener;
25+
26+
class BannerSnippets implements AppEventListener {
27+
28+
private AdView adView;
29+
private AdManagerAdView adManagerAdView;
30+
31+
public void setAdListener() {
32+
// [START ad_events]
33+
if (adView != null) {
34+
adView.setAdListener(
35+
new AdListener() {
36+
@Override
37+
public void onAdClicked() {
38+
// Code to be executed when the user clicks on an ad.
39+
}
40+
41+
@Override
42+
public void onAdClosed() {
43+
// Code to be executed when the user is about to return
44+
// to the app after tapping on an ad.
45+
}
46+
47+
@Override
48+
public void onAdFailedToLoad(@NonNull LoadAdError adError) {
49+
// Code to be executed when an ad request fails.
50+
}
51+
52+
@Override
53+
public void onAdImpression() {
54+
// Code to be executed when an impression is recorded
55+
// for an ad.
56+
}
57+
58+
@Override
59+
public void onAdLoaded() {
60+
// Code to be executed when an ad finishes loading.
61+
}
62+
63+
@Override
64+
public void onAdOpened() {
65+
// Code to be executed when an ad opens an overlay that
66+
// covers the screen.
67+
}
68+
});
69+
}
70+
// [END ad_events]
71+
}
72+
73+
// [START destroy]
74+
public void destroyBanner() {
75+
// Remove banner from view hierarchy.
76+
if (adView != null) {
77+
View parentView = (View) adView.getParent();
78+
if (parentView instanceof ViewGroup) {
79+
((ViewGroup) parentView).removeView(adView);
80+
}
81+
82+
// Destroy the banner ad resources.
83+
adView.destroy();
84+
}
85+
86+
// Drop reference to the banner ad.
87+
adView = null;
88+
}
89+
90+
// [END destroy]
91+
92+
public void manualImpressionCounting() {
93+
// [START enable_manual_impressions]
94+
if (adManagerAdView != null) {
95+
adManagerAdView.setManualImpressionsEnabled(true);
96+
}
97+
// [END enable_manual_impressions]
98+
}
99+
100+
public void recordManualImpression() {
101+
// [START record_manual_impressions]
102+
if (adManagerAdView != null) {
103+
adManagerAdView.recordManualImpression();
104+
}
105+
// [END record_manual_impressions]
106+
}
107+
108+
public void setAppEventListener() {
109+
// [START set_app_event_listener]
110+
if (adManagerAdView != null) {
111+
adManagerAdView.setAppEventListener(this);
112+
}
113+
// [END set_app_event_listener]
114+
}
115+
116+
// [START app_events]
117+
@Override
118+
public void onAppEvent(@NonNull String name, @NonNull String info) {
119+
if (name.equals("color")) {
120+
switch (info) {
121+
case "green":
122+
// Set background color to green.
123+
break;
124+
case "blue":
125+
// Set background color to blue.
126+
break;
127+
default:
128+
// Set background color to black.
129+
break;
130+
}
131+
}
132+
}
133+
// [END app_events]
134+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.android.gms.snippets
16+
17+
import android.view.ViewGroup
18+
import com.google.android.gms.ads.AdListener
19+
import com.google.android.gms.ads.AdView
20+
import com.google.android.gms.ads.LoadAdError
21+
import com.google.android.gms.ads.admanager.AdManagerAdView
22+
import com.google.android.gms.ads.admanager.AppEventListener
23+
24+
private class BannerSnippets : AppEventListener {
25+
26+
private var adView: AdView? = null
27+
private var adManagerAdView: AdManagerAdView? = null
28+
29+
fun setAdListener() {
30+
// [START ad_events]
31+
adView?.adListener =
32+
object : AdListener() {
33+
override fun onAdClicked() {
34+
// Code to be executed when the user clicks on an ad.
35+
}
36+
37+
override fun onAdClosed() {
38+
// Code to be executed when the user is about to return
39+
// to the app after tapping on an ad.
40+
}
41+
42+
override fun onAdFailedToLoad(adError: LoadAdError) {
43+
// Code to be executed when an ad request fails.
44+
}
45+
46+
override fun onAdImpression() {
47+
// Code to be executed when an impression is recorded
48+
// for an ad.
49+
}
50+
51+
override fun onAdLoaded() {
52+
// Code to be executed when an ad finishes loading.
53+
}
54+
55+
override fun onAdOpened() {
56+
// Code to be executed when an ad opens an overlay that
57+
// covers the screen.
58+
}
59+
}
60+
// [END ad_events]
61+
}
62+
63+
// [START destroy]
64+
fun destroyBanner() {
65+
// Remove banner from view hierarchy.
66+
val parentView = adView?.parent
67+
if (parentView is ViewGroup) {
68+
parentView.removeView(adView)
69+
}
70+
71+
// Destroy the banner ad resources.
72+
adView?.destroy()
73+
74+
// Drop reference to the banner ad.
75+
adView = null
76+
}
77+
78+
// [END destroy]
79+
80+
fun manualImpressionCounting() {
81+
// [START enable_manual_impressions]
82+
adManagerAdView?.setManualImpressionsEnabled(true)
83+
// [END enable_manual_impressions]
84+
}
85+
86+
fun recordManualImpression() {
87+
// [START record_manual_impressions]
88+
adManagerAdView?.recordManualImpression()
89+
// [END record_manual_impressions]
90+
}
91+
92+
fun setAppEventListener() {
93+
// [START set_app_event_listener]
94+
adManagerAdView?.appEventListener = this
95+
// [END set_app_event_listener]
96+
}
97+
98+
// [START app_events]
99+
override fun onAppEvent(name: String, info: String) {
100+
if (name == "color") {
101+
when (info) {
102+
"green" -> {
103+
// Set background color to green.
104+
}
105+
"blue" -> {
106+
// Set background color to blue.
107+
}
108+
else -> {
109+
// Set background color to black.
110+
}
111+
}
112+
}
113+
}
114+
// [END app_events]
115+
}

0 commit comments

Comments
 (0)