From 637e8e3329903bfcfbbdd20a3574a72771d00ae5 Mon Sep 17 00:00:00 2001 From: Nicholas Ventimiglia Date: Tue, 23 Apr 2024 16:21:06 -0700 Subject: [PATCH] Added Ad Choices Icon to Objective-C Custom Native Samples. PiperOrigin-RevId: 627537169 --- .../gms/nativeadsexample/MainActivity.java | 25 ++++++++++++++++--- .../res/layout/ad_simple_custom_template.xml | 9 ++++++- .../app/src/main/res/values/strings.xml | 1 + .../example/nativeadsexample/MainActivity.kt | 25 ++++++++++++------- .../res/layout/ad_simple_custom_template.xml | 9 ++++++- .../app/src/main/res/values/strings.xml | 1 + 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/java/admanager/NativeAdsExample/app/src/main/java/com/google/example/gms/nativeadsexample/MainActivity.java b/java/admanager/NativeAdsExample/app/src/main/java/com/google/example/gms/nativeadsexample/MainActivity.java index a8f81eeab..bf9957fbf 100644 --- a/java/admanager/NativeAdsExample/app/src/main/java/com/google/example/gms/nativeadsexample/MainActivity.java +++ b/java/admanager/NativeAdsExample/app/src/main/java/com/google/example/gms/nativeadsexample/MainActivity.java @@ -17,6 +17,7 @@ package com.google.example.gms.nativeadsexample; import android.annotation.SuppressLint; +import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.util.Log; @@ -43,6 +44,7 @@ import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; import com.google.android.gms.ads.nativead.MediaView; import com.google.android.gms.ads.nativead.NativeAd; +import com.google.android.gms.ads.nativead.NativeAdAssetNames; import com.google.android.gms.ads.nativead.NativeAdOptions; import com.google.android.gms.ads.nativead.NativeAdView; import com.google.android.gms.ads.nativead.NativeCustomFormatAd; @@ -56,7 +58,7 @@ public class MainActivity extends AppCompatActivity { private static final String AD_MANAGER_AD_UNIT_ID = "/6499/example/native"; - private static final String SIMPLE_TEMPLATE_ID = "10104090"; + private static final String SIMPLE_TEMPLATE_ID = "10063170"; private static final String TAG = "MainActivity"; private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false); @@ -272,13 +274,28 @@ public void onVideoEnd() { */ private void populateSimpleTemplateAdView( final NativeCustomFormatAd nativeCustomFormatAd, View adView) { - TextView headline = adView.findViewById(R.id.simplecustom_headline); - TextView caption = adView.findViewById(R.id.simplecustom_caption); + // Render the Ad Choices icon and click listener. + String adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW; + NativeAd.Image adChoiceAsset = nativeCustomFormatAd.getImage(adChoicesKey); + Drawable adChoicesDrawable = adChoiceAsset != null ? adChoiceAsset.getDrawable() : null; + + ImageView adChoicesIcon = adView.findViewById(R.id.simplecustom_adchoice); + adChoicesIcon.setImageDrawable(adChoicesDrawable); + adChoicesIcon.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + nativeCustomFormatAd.performClick(adChoicesKey); + } + }); + + TextView headline = adView.findViewById(R.id.simplecustom_headline); + TextView caption = adView.findViewById(R.id.simplecustom_caption); headline.setText(nativeCustomFormatAd.getText("Headline")); caption.setText(nativeCustomFormatAd.getText("Caption")); - FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder); + FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder); // Apps can check the MediaContent's hasVideoContent property to determine if the // NativeCustomFormatAd has a video asset. diff --git a/java/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml b/java/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml index bcaaf1bc1..eb7dbeb2b 100644 --- a/java/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml +++ b/java/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml @@ -10,7 +10,6 @@ android:id="@+id/simplecustom_headline" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginTop="16dp" android:gravity="center" android:textAppearance="?android:attr/textAppearanceLarge" /> @@ -32,4 +31,12 @@ android:textColor="#888888" android:textStyle="italic" /> + + diff --git a/java/admanager/NativeAdsExample/app/src/main/res/values/strings.xml b/java/admanager/NativeAdsExample/app/src/main/res/values/strings.xml index bb3097253..a55d06537 100644 --- a/java/admanager/NativeAdsExample/app/src/main/res/values/strings.xml +++ b/java/admanager/NativeAdsExample/app/src/main/res/values/strings.xml @@ -8,4 +8,5 @@ Request Unified Native Ads Start video ads muted Video status: + Ad Choices icon diff --git a/kotlin/admanager/NativeAdsExample/app/src/main/java/com/google/android/gms/example/nativeadsexample/MainActivity.kt b/kotlin/admanager/NativeAdsExample/app/src/main/java/com/google/android/gms/example/nativeadsexample/MainActivity.kt index faa7641af..3ef59f134 100644 --- a/kotlin/admanager/NativeAdsExample/app/src/main/java/com/google/android/gms/example/nativeadsexample/MainActivity.kt +++ b/kotlin/admanager/NativeAdsExample/app/src/main/java/com/google/android/gms/example/nativeadsexample/MainActivity.kt @@ -44,7 +44,7 @@ import java.util.concurrent.atomic.AtomicBoolean private const val TAG = "MainActivity" const val AD_MANAGER_AD_UNIT_ID = "/6499/example/native" -const val SIMPLE_TEMPLATE_ID = "10104090" +const val SIMPLE_TEMPLATE_ID = "10063170" /** A simple activity class that displays native ad formats. */ class MainActivity : AppCompatActivity() { @@ -91,7 +91,7 @@ class MainActivity : AppCompatActivity() { if (googleMobileAdsConsentManager.canRequestAds) { refreshAd( mainActivityBinding.nativeadsCheckbox.isChecked, - mainActivityBinding.customtemplateCheckbox.isChecked + mainActivityBinding.customtemplateCheckbox.isChecked, ) } } @@ -219,7 +219,7 @@ class MainActivity : AppCompatActivity() { String.format( Locale.getDefault(), "Video status: Ad contains a %.2f:1 video asset.", - mediaContent.aspectRatio + mediaContent.aspectRatio, ) // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The // VideoController will call methods on this object when events occur in the video @@ -245,7 +245,6 @@ class MainActivity : AppCompatActivity() { * particular "simple" custom native ad format. * * @param nativeCustomFormatAd the object containing the ad's assets - * @param adView the view to be populated */ private fun populateSimpleTemplateAdView(nativeCustomFormatAd: NativeCustomFormatAd) { customTemplateBinding.simplecustomHeadline.text = nativeCustomFormatAd.getText("Headline") @@ -269,6 +268,14 @@ class MainActivity : AppCompatActivity() { } } + // Render the Ad Choices icon and click listener. + val adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW + val adChoiceAsset = nativeCustomFormatAd.getImage(adChoicesKey) + customTemplateBinding.simplecustomAdchoice.setImageDrawable(choice?.drawable) + customTemplateBinding.simplecustomAdchoice.setOnClickListener { + nativeCustomFormatAd.performClick(adChoicesKey) + } + val mediaContent = nativeCustomFormatAd.mediaContent // Apps can check the MediaContent's hasVideoContent property to determine if the @@ -305,7 +312,7 @@ class MainActivity : AppCompatActivity() { Toast.makeText( this, "At least one ad format must be checked to request an ad.", - Toast.LENGTH_SHORT + Toast.LENGTH_SHORT, ) .show() return @@ -363,10 +370,10 @@ class MainActivity : AppCompatActivity() { Toast.makeText( this@MainActivity, "A custom click has occurred in the simple template", - Toast.LENGTH_SHORT + Toast.LENGTH_SHORT, ) .show() - } + }, ) } @@ -390,7 +397,7 @@ class MainActivity : AppCompatActivity() { Toast.makeText( this@MainActivity, "Failed to load native ad with error $error", - Toast.LENGTH_SHORT + Toast.LENGTH_SHORT, ) .show() } @@ -413,7 +420,7 @@ class MainActivity : AppCompatActivity() { // Load an ad. refreshAd( mainActivityBinding.nativeadsCheckbox.isChecked, - mainActivityBinding.customtemplateCheckbox.isChecked + mainActivityBinding.customtemplateCheckbox.isChecked, ) } } diff --git a/kotlin/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml b/kotlin/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml index bcaaf1bc1..eb7dbeb2b 100644 --- a/kotlin/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml +++ b/kotlin/admanager/NativeAdsExample/app/src/main/res/layout/ad_simple_custom_template.xml @@ -10,7 +10,6 @@ android:id="@+id/simplecustom_headline" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginTop="16dp" android:gravity="center" android:textAppearance="?android:attr/textAppearanceLarge" /> @@ -32,4 +31,12 @@ android:textColor="#888888" android:textStyle="italic" /> + + diff --git a/kotlin/admanager/NativeAdsExample/app/src/main/res/values/strings.xml b/kotlin/admanager/NativeAdsExample/app/src/main/res/values/strings.xml index 040fd60e7..7ca8f6e2d 100644 --- a/kotlin/admanager/NativeAdsExample/app/src/main/res/values/strings.xml +++ b/kotlin/admanager/NativeAdsExample/app/src/main/res/values/strings.xml @@ -3,4 +3,5 @@ Ad Manager Native Advanced More Privacy Settings + Ad Choices icon