Skip to content

Commit fdcbc0e

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added Ad Choices Icon to Objective-C Custom Native Samples.
PiperOrigin-RevId: 627537169
1 parent 8a8251f commit fdcbc0e

File tree

7 files changed

+68
-15
lines changed

7 files changed

+68
-15
lines changed

java/admanager/NativeAdsExample/app/src/main/java/com/google/example/gms/nativeadsexample/MainActivity.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.example.gms.nativeadsexample;
1818

1919
import android.annotation.SuppressLint;
20+
import android.graphics.drawable.Drawable;
2021
import android.os.Build;
2122
import android.os.Bundle;
2223
import android.util.Log;
@@ -43,6 +44,7 @@
4344
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
4445
import com.google.android.gms.ads.nativead.MediaView;
4546
import com.google.android.gms.ads.nativead.NativeAd;
47+
import com.google.android.gms.ads.nativead.NativeAdAssetNames;
4648
import com.google.android.gms.ads.nativead.NativeAdOptions;
4749
import com.google.android.gms.ads.nativead.NativeAdView;
4850
import com.google.android.gms.ads.nativead.NativeCustomFormatAd;
@@ -56,7 +58,7 @@
5658
public class MainActivity extends AppCompatActivity {
5759

5860
private static final String AD_MANAGER_AD_UNIT_ID = "/6499/example/native";
59-
private static final String SIMPLE_TEMPLATE_ID = "10104090";
61+
private static final String SIMPLE_TEMPLATE_ID = "10063170";
6062
private static final String TAG = "MainActivity";
6163

6264
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
@@ -272,13 +274,33 @@ public void onVideoEnd() {
272274
*/
273275
private void populateSimpleTemplateAdView(
274276
final NativeCustomFormatAd nativeCustomFormatAd, View adView) {
275-
TextView headline = adView.findViewById(R.id.simplecustom_headline);
276-
TextView caption = adView.findViewById(R.id.simplecustom_caption);
277277

278+
// Render the AdChoices image.
279+
String adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW;
280+
NativeAd.Image adChoiceAsset = nativeCustomFormatAd.getImage(adChoicesKey);
281+
Drawable adChoicesDrawable = adChoiceAsset != null ? adChoiceAsset.getDrawable() : null;
282+
ImageView adChoicesIcon = adView.findViewById(R.id.simplecustom_adchoices);
283+
if (adChoicesDrawable == null) {
284+
adChoicesIcon.setVisibility(View.GONE);
285+
} else {
286+
adChoicesIcon.setVisibility(View.VISIBLE);
287+
adChoicesIcon.setImageDrawable(adChoicesDrawable);
288+
// Enable clicks on AdChoices.
289+
adChoicesIcon.setOnClickListener(
290+
new View.OnClickListener() {
291+
@Override
292+
public void onClick(View v) {
293+
nativeCustomFormatAd.performClick(adChoicesKey);
294+
}
295+
});
296+
}
297+
298+
TextView headline = adView.findViewById(R.id.simplecustom_headline);
299+
TextView caption = adView.findViewById(R.id.simplecustom_caption);
278300
headline.setText(nativeCustomFormatAd.getText("Headline"));
279301
caption.setText(nativeCustomFormatAd.getText("Caption"));
280302

281-
FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder);
303+
FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder);
282304

283305
// Apps can check the MediaContent's hasVideoContent property to determine if the
284306
// NativeCustomFormatAd has a video asset.

java/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
android:id="@+id/simplecustom_headline"
1111
android:layout_width="match_parent"
1212
android:layout_height="wrap_content"
13-
android:layout_marginTop="16dp"
1413
android:gravity="center"
1514
android:textAppearance="?android:attr/textAppearanceLarge" />
1615

@@ -32,4 +31,12 @@
3231
android:textColor="#888888"
3332
android:textStyle="italic" />
3433

34+
<ImageView
35+
android:id="@+id/simplecustom_adchoices"
36+
android:layout_width="15dp"
37+
android:layout_height="15dp"
38+
android:layout_gravity="end"
39+
android:adjustViewBounds="true"
40+
android:contentDescription="@string/ad_choices" />
41+
3542
</LinearLayout>

java/admanager/NativeAdsExample/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<resources>
22
<string name="ad_attribution" translatable="false">Ad</string>
3+
<string name="ad_choices" translatable="false">Ad Choices icon</string>
34
<string name="app_name" translatable="false">Ad Manager Native Advanced</string>
45
<string name="more_menu" translatable="false">More</string>
56
<string name="privacy_settings" translatable="false">Privacy Settings</string>

kotlin/admanager/NativeAdsExample/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
<application
55
android:allowBackup="true"
6+
android:taskAffinity=""
67
android:icon="@mipmap/ic_launcher"
78
android:label="@string/app_name"
89
android:theme="@style/AppTheme">

kotlin/admanager/NativeAdsExample/app/src/main/java/com/google/android/gms/example/nativeadsexample/MainActivity.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import com.google.android.gms.ads.VideoOptions
3333
import com.google.android.gms.ads.admanager.AdManagerAdRequest
3434
import com.google.android.gms.ads.nativead.MediaView
3535
import com.google.android.gms.ads.nativead.NativeAd
36+
import com.google.android.gms.ads.nativead.NativeAdAssetNames
3637
import com.google.android.gms.ads.nativead.NativeAdOptions
3738
import com.google.android.gms.ads.nativead.NativeAdView
3839
import com.google.android.gms.ads.nativead.NativeCustomFormatAd
@@ -44,7 +45,7 @@ import java.util.concurrent.atomic.AtomicBoolean
4445

4546
private const val TAG = "MainActivity"
4647
const val AD_MANAGER_AD_UNIT_ID = "/6499/example/native"
47-
const val SIMPLE_TEMPLATE_ID = "10104090"
48+
const val SIMPLE_TEMPLATE_ID = "10063170"
4849

4950
/** A simple activity class that displays native ad formats. */
5051
class MainActivity : AppCompatActivity() {
@@ -91,7 +92,7 @@ class MainActivity : AppCompatActivity() {
9192
if (googleMobileAdsConsentManager.canRequestAds) {
9293
refreshAd(
9394
mainActivityBinding.nativeadsCheckbox.isChecked,
94-
mainActivityBinding.customtemplateCheckbox.isChecked
95+
mainActivityBinding.customtemplateCheckbox.isChecked,
9596
)
9697
}
9798
}
@@ -219,7 +220,7 @@ class MainActivity : AppCompatActivity() {
219220
String.format(
220221
Locale.getDefault(),
221222
"Video status: Ad contains a %.2f:1 video asset.",
222-
mediaContent.aspectRatio
223+
mediaContent.aspectRatio,
223224
)
224225
// Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
225226
// VideoController will call methods on this object when events occur in the video
@@ -245,7 +246,6 @@ class MainActivity : AppCompatActivity() {
245246
* particular "simple" custom native ad format.
246247
*
247248
* @param nativeCustomFormatAd the object containing the ad's assets
248-
* @param adView the view to be populated
249249
*/
250250
private fun populateSimpleTemplateAdView(nativeCustomFormatAd: NativeCustomFormatAd) {
251251
customTemplateBinding.simplecustomHeadline.text = nativeCustomFormatAd.getText("Headline")
@@ -269,6 +269,20 @@ class MainActivity : AppCompatActivity() {
269269
}
270270
}
271271

272+
// Render the AdChoices image.
273+
val adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW
274+
val adChoiceAsset = nativeCustomFormatAd.getImage(adChoicesKey)
275+
if (adChoiceAsset == null) {
276+
customTemplateBinding.simplecustomAdchoices.visibility = View.GONE
277+
} else {
278+
customTemplateBinding.simplecustomAdchoices.setImageDrawable(adChoiceAsset.drawable)
279+
customTemplateBinding.simplecustomAdchoices.visibility = View.VISIBLE
280+
// Enable clicks on AdChoices.
281+
customTemplateBinding.simplecustomAdchoices.setOnClickListener {
282+
nativeCustomFormatAd.performClick(adChoicesKey)
283+
}
284+
}
285+
272286
val mediaContent = nativeCustomFormatAd.mediaContent
273287

274288
// Apps can check the MediaContent's hasVideoContent property to determine if the
@@ -305,7 +319,7 @@ class MainActivity : AppCompatActivity() {
305319
Toast.makeText(
306320
this,
307321
"At least one ad format must be checked to request an ad.",
308-
Toast.LENGTH_SHORT
322+
Toast.LENGTH_SHORT,
309323
)
310324
.show()
311325
return
@@ -363,10 +377,10 @@ class MainActivity : AppCompatActivity() {
363377
Toast.makeText(
364378
this@MainActivity,
365379
"A custom click has occurred in the simple template",
366-
Toast.LENGTH_SHORT
380+
Toast.LENGTH_SHORT,
367381
)
368382
.show()
369-
}
383+
},
370384
)
371385
}
372386

@@ -390,7 +404,7 @@ class MainActivity : AppCompatActivity() {
390404
Toast.makeText(
391405
this@MainActivity,
392406
"Failed to load native ad with error $error",
393-
Toast.LENGTH_SHORT
407+
Toast.LENGTH_SHORT,
394408
)
395409
.show()
396410
}
@@ -413,7 +427,7 @@ class MainActivity : AppCompatActivity() {
413427
// Load an ad.
414428
refreshAd(
415429
mainActivityBinding.nativeadsCheckbox.isChecked,
416-
mainActivityBinding.customtemplateCheckbox.isChecked
430+
mainActivityBinding.customtemplateCheckbox.isChecked,
417431
)
418432
}
419433
}

kotlin/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
android:id="@+id/simplecustom_headline"
1111
android:layout_width="match_parent"
1212
android:layout_height="wrap_content"
13-
android:layout_marginTop="16dp"
1413
android:gravity="center"
1514
android:textAppearance="?android:attr/textAppearanceLarge" />
1615

@@ -32,4 +31,12 @@
3231
android:textColor="#888888"
3332
android:textStyle="italic" />
3433

34+
<ImageView
35+
android:id="@+id/simplecustom_adchoices"
36+
android:layout_width="15dp"
37+
android:layout_height="15dp"
38+
android:layout_gravity="end"
39+
android:adjustViewBounds="true"
40+
android:contentDescription="@string/ad_choices" />
41+
3542
</LinearLayout>

kotlin/admanager/NativeAdsExample/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<resources>
22
<string name="ad_attribution" translatable="false">Ad</string>
3+
<string name="ad_choices" translatable="false">Ad Choices icon</string>
34
<string name="app_name" translatable="false">Ad Manager Native Advanced</string>
45
<string name="more_menu" translatable="false">More</string>
56
<string name="privacy_settings" translatable="false">Privacy Settings</string>

0 commit comments

Comments
 (0)