Skip to content

Commit

Permalink
Added Ad Choices Icon to Java Custom Native Samples.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 633737732
  • Loading branch information
nventimigli authored and Copybara-Service committed May 14, 2024
1 parent a004ab4 commit 0feec49
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand All @@ -32,4 +31,12 @@
android:textColor="#888888"
android:textStyle="italic" />

<ImageView
android:id="@+id/simplecustom_adchoices"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="end"
android:adjustViewBounds="true"
android:contentDescription="@string/ad_choices" />

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<resources>
<string name="ad_attribution" translatable="false">Ad</string>
<string name="ad_choices" translatable="false">Ad Choices icon</string>
<string name="app_name" translatable="false">Ad Manager Native Advanced</string>
<string name="more_menu" translatable="false">More</string>
<string name="privacy_settings" translatable="false">Privacy Settings</string>
Expand Down

0 comments on commit 0feec49

Please sign in to comment.