Skip to content

Commit

Permalink
Added Ad Choices Icon to Swift Custom Native Samples.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 627544078
  • Loading branch information
nventimigli authored and Copybara-Service committed Apr 25, 2024
1 parent 4aa0d67 commit 6395596
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 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,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.
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_adchoice"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="end"
android:adjustViewBounds="true"
android:contentDescription="@string/ad_choices" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="request_unified" translatable="false">Request Unified Native Ads</string>
<string name="start_muted" translatable="false">Start video ads muted</string>
<string name="video_status" translatable="false">Video status: </string>
<string name="ad_choices" translatable="false">Ad Choices icon</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -91,7 +91,7 @@ class MainActivity : AppCompatActivity() {
if (googleMobileAdsConsentManager.canRequestAds) {
refreshAd(
mainActivityBinding.nativeadsCheckbox.isChecked,
mainActivityBinding.customtemplateCheckbox.isChecked
mainActivityBinding.customtemplateCheckbox.isChecked,
)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
},
)
}

Expand All @@ -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()
}
Expand All @@ -413,7 +420,7 @@ class MainActivity : AppCompatActivity() {
// Load an ad.
refreshAd(
mainActivityBinding.nativeadsCheckbox.isChecked,
mainActivityBinding.customtemplateCheckbox.isChecked
mainActivityBinding.customtemplateCheckbox.isChecked,
)
}
}
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_adchoice"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="end"
android:adjustViewBounds="true"
android:contentDescription="@string/ad_choices" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<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>
<string name="ad_choices" translatable="false">Ad Choices icon</string>
</resources>

0 comments on commit 6395596

Please sign in to comment.