Skip to content

Commit 6395596

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added Ad Choices Icon to Swift Custom Native Samples.
PiperOrigin-RevId: 627544078
1 parent 4aa0d67 commit 6395596

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

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

Lines changed: 21 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,28 @@ 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 Ad Choices icon and click listener.
279+
String adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW;
280+
NativeAd.Image adChoiceAsset = nativeCustomFormatAd.getImage(adChoicesKey);
281+
Drawable adChoicesDrawable = adChoiceAsset != null ? adChoiceAsset.getDrawable() : null;
282+
283+
ImageView adChoicesIcon = adView.findViewById(R.id.simplecustom_adchoice);
284+
adChoicesIcon.setImageDrawable(adChoicesDrawable);
285+
adChoicesIcon.setOnClickListener(
286+
new View.OnClickListener() {
287+
@Override
288+
public void onClick(View v) {
289+
nativeCustomFormatAd.performClick(adChoicesKey);
290+
}
291+
});
292+
293+
TextView headline = adView.findViewById(R.id.simplecustom_headline);
294+
TextView caption = adView.findViewById(R.id.simplecustom_caption);
278295
headline.setText(nativeCustomFormatAd.getText("Headline"));
279296
caption.setText(nativeCustomFormatAd.getText("Caption"));
280297

281-
FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder);
298+
FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder);
282299

283300
// Apps can check the MediaContent's hasVideoContent property to determine if the
284301
// 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_adchoice"
36+
android:layout_width="20dp"
37+
android:layout_height="20dp"
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
@@ -8,4 +8,5 @@
88
<string name="request_unified" translatable="false">Request Unified Native Ads</string>
99
<string name="start_muted" translatable="false">Start video ads muted</string>
1010
<string name="video_status" translatable="false">Video status: </string>
11+
<string name="ad_choices" translatable="false">Ad Choices icon</string>
1112
</resources>

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import java.util.concurrent.atomic.AtomicBoolean
4444

4545
private const val TAG = "MainActivity"
4646
const val AD_MANAGER_AD_UNIT_ID = "/6499/example/native"
47-
const val SIMPLE_TEMPLATE_ID = "10104090"
47+
const val SIMPLE_TEMPLATE_ID = "10063170"
4848

4949
/** A simple activity class that displays native ad formats. */
5050
class MainActivity : AppCompatActivity() {
@@ -91,7 +91,7 @@ class MainActivity : AppCompatActivity() {
9191
if (googleMobileAdsConsentManager.canRequestAds) {
9292
refreshAd(
9393
mainActivityBinding.nativeadsCheckbox.isChecked,
94-
mainActivityBinding.customtemplateCheckbox.isChecked
94+
mainActivityBinding.customtemplateCheckbox.isChecked,
9595
)
9696
}
9797
}
@@ -219,7 +219,7 @@ class MainActivity : AppCompatActivity() {
219219
String.format(
220220
Locale.getDefault(),
221221
"Video status: Ad contains a %.2f:1 video asset.",
222-
mediaContent.aspectRatio
222+
mediaContent.aspectRatio,
223223
)
224224
// Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
225225
// VideoController will call methods on this object when events occur in the video
@@ -245,7 +245,6 @@ class MainActivity : AppCompatActivity() {
245245
* particular "simple" custom native ad format.
246246
*
247247
* @param nativeCustomFormatAd the object containing the ad's assets
248-
* @param adView the view to be populated
249248
*/
250249
private fun populateSimpleTemplateAdView(nativeCustomFormatAd: NativeCustomFormatAd) {
251250
customTemplateBinding.simplecustomHeadline.text = nativeCustomFormatAd.getText("Headline")
@@ -269,6 +268,14 @@ class MainActivity : AppCompatActivity() {
269268
}
270269
}
271270

271+
// Render the Ad Choices icon and click listener.
272+
val adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW
273+
val adChoiceAsset = nativeCustomFormatAd.getImage(adChoicesKey)
274+
customTemplateBinding.simplecustomAdchoice.setImageDrawable(choice?.drawable)
275+
customTemplateBinding.simplecustomAdchoice.setOnClickListener {
276+
nativeCustomFormatAd.performClick(adChoicesKey)
277+
}
278+
272279
val mediaContent = nativeCustomFormatAd.mediaContent
273280

274281
// Apps can check the MediaContent's hasVideoContent property to determine if the
@@ -305,7 +312,7 @@ class MainActivity : AppCompatActivity() {
305312
Toast.makeText(
306313
this,
307314
"At least one ad format must be checked to request an ad.",
308-
Toast.LENGTH_SHORT
315+
Toast.LENGTH_SHORT,
309316
)
310317
.show()
311318
return
@@ -363,10 +370,10 @@ class MainActivity : AppCompatActivity() {
363370
Toast.makeText(
364371
this@MainActivity,
365372
"A custom click has occurred in the simple template",
366-
Toast.LENGTH_SHORT
373+
Toast.LENGTH_SHORT,
367374
)
368375
.show()
369-
}
376+
},
370377
)
371378
}
372379

@@ -390,7 +397,7 @@ class MainActivity : AppCompatActivity() {
390397
Toast.makeText(
391398
this@MainActivity,
392399
"Failed to load native ad with error $error",
393-
Toast.LENGTH_SHORT
400+
Toast.LENGTH_SHORT,
394401
)
395402
.show()
396403
}
@@ -413,7 +420,7 @@ class MainActivity : AppCompatActivity() {
413420
// Load an ad.
414421
refreshAd(
415422
mainActivityBinding.nativeadsCheckbox.isChecked,
416-
mainActivityBinding.customtemplateCheckbox.isChecked
423+
mainActivityBinding.customtemplateCheckbox.isChecked,
417424
)
418425
}
419426
}

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_adchoice"
36+
android:layout_width="20dp"
37+
android:layout_height="20dp"
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
@@ -3,4 +3,5 @@
33
<string name="app_name" translatable="false">Ad Manager Native Advanced</string>
44
<string name="more_menu" translatable="false">More</string>
55
<string name="privacy_settings" translatable="false">Privacy Settings</string>
6+
<string name="ad_choices" translatable="false">Ad Choices icon</string>
67
</resources>

0 commit comments

Comments
 (0)