From fa8578e19730d1c77cda378ff1495502a7967307 Mon Sep 17 00:00:00 2001 From: Nicholas Ventimiglia Date: Tue, 23 Apr 2024 16:48:32 -0700 Subject: [PATCH] Added Ad Choices Icon to Swift Custom Native Samples. PiperOrigin-RevId: 627544078 --- .../gms/nativeadsexample/MainActivity.java | 30 ++++++++++++++--- .../res/layout/ad_simple_custom_template.xml | 9 +++++- .../app/src/main/res/values/strings.xml | 1 + .../app/src/main/AndroidManifest.xml | 1 + .../example/nativeadsexample/MainActivity.kt | 32 +++++++++++++------ .../res/layout/ad_simple_custom_template.xml | 9 +++++- .../app/src/main/res/values/strings.xml | 1 + 7 files changed, 68 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..c0757371c 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,33 @@ 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 AdChoices image. + 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_adchoices); + if (adChoicesDrawable == null) { + adChoicesIcon.setVisibility(View.GONE); + } else { + adChoicesIcon.setVisibility(View.VISIBLE); + adChoicesIcon.setImageDrawable(adChoicesDrawable); + // Enable clicks on AdChoices. + 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..43c11a18f 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..451cfe639 100644 --- a/java/admanager/NativeAdsExample/app/src/main/res/values/strings.xml +++ b/java/admanager/NativeAdsExample/app/src/main/res/values/strings.xml @@ -1,5 +1,6 @@ Ad + Ad Choices icon Ad Manager Native Advanced More Privacy Settings diff --git a/kotlin/admanager/NativeAdsExample/app/src/main/AndroidManifest.xml b/kotlin/admanager/NativeAdsExample/app/src/main/AndroidManifest.xml index 43ed593ed..bc1ab925f 100644 --- a/kotlin/admanager/NativeAdsExample/app/src/main/AndroidManifest.xml +++ b/kotlin/admanager/NativeAdsExample/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ 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..e282e3413 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 @@ -33,6 +33,7 @@ import com.google.android.gms.ads.VideoOptions import com.google.android.gms.ads.admanager.AdManagerAdRequest 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 @@ -44,7 +45,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 +92,7 @@ class MainActivity : AppCompatActivity() { if (googleMobileAdsConsentManager.canRequestAds) { refreshAd( mainActivityBinding.nativeadsCheckbox.isChecked, - mainActivityBinding.customtemplateCheckbox.isChecked + mainActivityBinding.customtemplateCheckbox.isChecked, ) } } @@ -219,7 +220,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 +246,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 +269,20 @@ class MainActivity : AppCompatActivity() { } } + // Render the AdChoices image. + val adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW + val adChoiceAsset = nativeCustomFormatAd.getImage(adChoicesKey) + if (adChoiceAsset == null) { + customTemplateBinding.simplecustomAdchoices.visibility = View.GONE + } else { + customTemplateBinding.simplecustomAdchoices.setImageDrawable(adChoiceAsset.drawable) + customTemplateBinding.simplecustomAdchoices.visibility = View.VISIBLE + // Enable clicks on AdChoices. + customTemplateBinding.simplecustomAdchoices.setOnClickListener { + nativeCustomFormatAd.performClick(adChoicesKey) + } + } + val mediaContent = nativeCustomFormatAd.mediaContent // Apps can check the MediaContent's hasVideoContent property to determine if the @@ -305,7 +319,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 +377,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 +404,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 +427,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..43c11a18f 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..317b4921d 100644 --- a/kotlin/admanager/NativeAdsExample/app/src/main/res/values/strings.xml +++ b/kotlin/admanager/NativeAdsExample/app/src/main/res/values/strings.xml @@ -1,5 +1,6 @@ Ad + Ad Choices icon Ad Manager Native Advanced More Privacy Settings