Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Ad Choices Icon to Objective-C Custom Native Samples. #671

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<application
android:allowBackup="true"
android:taskAffinity=""
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -91,7 +92,7 @@ class MainActivity : AppCompatActivity() {
if (googleMobileAdsConsentManager.canRequestAds) {
refreshAd(
mainActivityBinding.nativeadsCheckbox.isChecked,
mainActivityBinding.customtemplateCheckbox.isChecked
mainActivityBinding.customtemplateCheckbox.isChecked,
)
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
},
)
}

Expand All @@ -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()
}
Expand All @@ -413,7 +427,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_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
Loading