जब कोई नेटिव विज्ञापन लोड होता है, तो Google Mobile Ads SDK (बीटा वर्शन), विज्ञापन के उस फ़ॉर्मैट के लिए लिसनर को शुरू करता है. इसके बाद, विज्ञापन दिखाने की ज़िम्मेदारी आपके ऐप्लिकेशन की होती है. हालांकि, यह ज़रूरी नहीं है कि ऐप्लिकेशन तुरंत विज्ञापन दिखाए. सिस्टम की ओर से तय किए गए विज्ञापन फ़ॉर्मैट को आसानी से दिखाने के लिए, एसडीके कुछ काम के संसाधन उपलब्ध कराता है. इनके बारे में यहां बताया गया है.
NativeAdView
क्लास को परिभाषित करना
NativeAdView
क्लास को परिभाषित करें. यह क्लास, एक ViewGroup
क्लास है. साथ ही, यह NativeAdView
क्लास के लिए टॉप लेवल का कंटेनर है. हर नेटिव विज्ञापन व्यू में नेटिव विज्ञापन ऐसेट होती हैं. जैसे, MediaView
व्यू एलिमेंट या Title
व्यू एलिमेंट. ये NativeAdView
ऑब्जेक्ट के चाइल्ड होने चाहिए.
एक्सएमएल लेआउट
अपने प्रोजेक्ट में कोई XML NativeAdView
जोड़ें:
<com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal">
<ImageView
android:id="@+id/ad_app_icon" />
<TextView
android:id="@+id/ad_headline" />
</LinearLayout>
<!--Add remaining assets such as the image and media view.-->
</LinearLayout>
</com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView>
Jetpack Compose
JetpackComposeDemo/compose-util मॉड्यूल शामिल करें. इसमें NativeAdView
और उसकी ऐसेट को कंपोज़ करने के लिए हेल्पर शामिल हैं.
compose-util
मॉड्यूल का इस्तेमाल करके, NativeAdView
लिखें:
import com.google.android.gms.compose_util.NativeAdAttribution
import com.google.android.gms.compose_util.NativeAdView
@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
NativeAdView {
// Display the ad attribution.
NativeAdAttribution(text = context.getString("Ad"))
// Add remaining assets such as the image and media view.
}
}
लोड किए गए नेटिव विज्ञापन को मैनेज करना
नेटिव विज्ञापन लोड होने पर, कॉलबैक इवेंट को मैनेज करें, नेटिव विज्ञापन व्यू को बड़ा करें, और उसे व्यू हैरारकी में जोड़ें:
Kotlin
// Build an ad request with native ad options to customize the ad.
val adTypes = listOf(NativeAd.NativeAdType.NATIVE)
val adRequest = NativeAdRequest
.Builder("ca-app-pub-3940256099942544/2247696110", adTypes)
.build()
val adCallback =
object : NativeAdLoaderCallback {
override fun onNativeAdLoaded(nativeAd: NativeAd) {
activity?.runOnUiThread {
val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)
val adView = nativeAdBinding.root
val frameLayout = myActivityLayout.nativeAdPlaceholder
// Populate and register the native ad asset views.
displayNativeAd(nativeAd, nativeAdBinding)
// Remove all old ad views and add the new native ad
// view to the view hierarchy.
frameLayout.removeAllViews()
frameLayout.addView(adView)
}
}
}
// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback)
Java
// Build an ad request with native ad options to customize the ad.
List<NativeAd.NativeAdType> adTypes = Arrays.asList(NativeAd.NativeAdType.NATIVE);
NativeAdRequest adRequest = new NativeAdRequest
.Builder("ca-app-pub-3940256099942544/2247696110", adTypes)
.build();
NativeAdLoaderCallback adCallback = new NativeAdLoaderCallback() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
if (getActivity() != null) {
getActivity()
.runOnUiThread(() -> {
// Inflate the native ad view and add it to the view hierarchy.
NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());
NativeAdView adView = (NativeAdView) nativeAdBinding.getRoot();
FrameLayout frameLayout = myActivityLayout.nativeAdPlaceholder;
// Populate and register the native ad asset views.
displayNativeAd(nativeAd, nativeAdBinding);
// Remove all old ad views and add the new native ad
// view to the view hierarchy.
frameLayout.removeAllViews();
frameLayout.addView(adView);
});
}
}
};
// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback);
ध्यान दें कि किसी नेटिव विज्ञापन की सभी ऐसेट, NativeAdView
लेआउट में रेंडर की जानी चाहिए. Google Mobile Ads SDK (बीटा), नेटिव ऐसेट को नेटिव विज्ञापन व्यू लेआउट से बाहर रेंडर किए जाने पर चेतावनी लॉग करने की कोशिश करता है.
विज्ञापन व्यू क्लास, हर ऐसेट के लिए इस्तेमाल किए गए व्यू को रजिस्टर करने के तरीके भी उपलब्ध कराती हैं. साथ ही, NativeAd
ऑब्जेक्ट को रजिस्टर करने का तरीका भी उपलब्ध कराती हैं.
इस तरह से व्यू रजिस्टर करने पर, SDK टूल इन जैसे टास्क अपने-आप मैनेज कर सकता है:
- रिकॉर्डिंग पर क्लिक
- स्क्रीन पर पहला पिक्सल दिखने पर इंप्रेशन रिकॉर्ड करना
- AdChoices ओवरले दिखाना
नेटिव विज्ञापन दिखाना
यहां दिए गए उदाहरण में, नेटिव विज्ञापन दिखाने का तरीका बताया गया है:
Kotlin
private fun displayNativeAd(nativeAd: NativeAd, nativeAdBinding : NativeAdBinding) {
// Set the native ad view elements.
val nativeAdView = nativeAdBinding.root
nativeAdView.advertiserView = nativeAdBinding.adAdvertiser
nativeAdView.bodyView = nativeAdBinding.adBody
nativeAdView.callToActionView = nativeAdBinding.adCallToAction
nativeAdView.headlineView = nativeAdBinding.adHeadline
nativeAdView.iconView = nativeAdBinding.adAppIcon
nativeAdView.priceView = nativeAdBinding.adPrice
nativeAdView.starRatingView = nativeAdBinding.adStars
nativeAdView.storeView = nativeAdBinding.adStore
// Set the view element with the native ad assets.
nativeAdBinding.adAdvertiser.text = nativeAd.advertiser
nativeAdBinding.adBody.text = nativeAd.body
nativeAdBinding.adCallToAction.text = nativeAd.callToAction
nativeAdBinding.adHeadline.text = nativeAd.headline
nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.icon?.drawable)
nativeAdBinding.adPrice.text = nativeAd.price
nativeAd.starRating?.toFloat().let { value ->
nativeAdBinding.adStars.rating = value
}
nativeAdBinding.adStore.text = nativeAd.store
// Hide views for assets that don't have data.
nativeAdBinding.adAdvertiser.visibility = getAssetViewVisibility(nativeAd.advertiser)
nativeAdBinding.adBody.visibility = getAssetViewVisibility(nativeAd.body)
nativeAdBinding.adCallToAction.visibility = getAssetViewVisibility(nativeAd.callToAction)
nativeAdBinding.adHeadline.visibility = getAssetViewVisibility(nativeAd.headline)
nativeAdBinding.adAppIcon.visibility = getAssetViewVisibility(nativeAd.icon)
nativeAdBinding.adPrice.visibility = getAssetViewVisibility(nativeAd.price)
nativeAdBinding.adStars.visibility = getAssetViewVisibility(nativeAd.starRating)
nativeAdBinding.adStore.visibility = getAssetViewVisibility(nativeAd.store)
// Inform Google Mobile Ads SDK (beta) that you have finished populating
// the native ad views with this native ad.
nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia)
}
/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return [View.VISIBLE] if the asset is not null, [View.INVISIBLE] otherwise.
*/
private fun getAssetViewVisibility(asset: Any?): Int {
return if (asset == null) View.INVISIBLE else View.VISIBLE
}
Java
private void displayNativeAd(ad: NativeAd, nativeAdBinding : NativeAdBinding) {
// Set the native ad view elements.
NativeAdView nativeAdView = nativeAdBinding.getRoot();
nativeAdView.setAdvertiserView(nativeAdBinding.adAdvertiser);
nativeAdView.setBodyView(nativeAdBinding.adBody);
nativeAdView.setCallToActionView(nativeAdBinding.adCallToAction);
nativeAdView.setHeadlineView(nativeAdBinding.adHeadline);
nativeAdView.setIconView(nativeAdBinding.adAppIcon);
nativeAdView.setPriceView(nativeAdBinding.adPrice);
nativeAdView.setStarRatingView(nativeAdBinding.adStars);
nativeAdView.setStoreView(nativeAdBinding.adStore);
// Set the view element with the native ad assets.
nativeAdBinding.adAdvertiser.setText(nativeAd.getAdvertiser());
nativeAdBinding.adBody.setText(nativeAd.getBody());
nativeAdBinding.adCallToAction.setText(nativeAd.getCallToAction());
nativeAdBinding.adHeadline.setText(nativeAd.getHeadline());
if (nativeAd.getIcon() != null) {
nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.getIcon().getDrawable());
}
nativeAdBinding.adPrice.setText(nativeAd.getPrice());
if (nativeAd.getStarRating() != null) {
nativeAdBinding.adStars.setRating(nativeAd.getStarRating().floatValue());
}
nativeAdBinding.adStore.setText(nativeAd.getStore());
// Hide views for assets that don't have data.
nativeAdBinding.adAdvertiser.setVisibility(getAssetViewVisibility(nativeAd.getAdvertiser()));
nativeAdBinding.adBody.setVisibility(getAssetViewVisibility(nativeAd.getBody()));
nativeAdBinding.adCallToAction.setVisibility(getAssetViewVisibility(nativeAd.getCallToAction()));
nativeAdBinding.adHeadline.setVisibility(getAssetViewVisibility(nativeAd.getHeadline()));
nativeAdBinding.adAppIcon.setVisibility(getAssetViewVisibility(nativeAd.getIcon()));
nativeAdBinding.adPrice.setVisibility(getAssetViewVisibility(nativeAd.getPrice()));
nativeAdBinding.adStars.setVisibility(getAssetViewVisibility(nativeAd.getStarRating()));
nativeAdBinding.adStore.setVisibility(getAssetViewVisibility(nativeAd.getStore()));
// Inform Google Mobile Ads SDK (beta) that you have finished populating
// the native ad views with this native ad.
nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia);
}
/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return {@link View#VISIBLE} if the asset is not null, {@link View#INVISIBLE} otherwise.
*/
private int getAssetViewVisibility(Object asset) {
return (asset == null) ? View.INVISIBLE : View.VISIBLE;
}
AdChoices ओवरले
SDK टूल, हर विज्ञापन व्यू में AdChoices ओवरले जोड़ता है. AdChoices लोगो को अपने-आप डालने के लिए, नेटिव विज्ञापन व्यू के पसंदीदा कोने में जगह छोड़ें. यह भी ज़रूरी है कि AdChoices ओवरले आसानी से दिखे. इसलिए, बैकग्राउंड के लिए सही रंग और इमेज चुनें. ओवरले के दिखने और काम करने के तरीके के बारे में ज़्यादा जानने के लिए, नेटिव विज्ञापनों के फ़ील्ड की जानकारी लेख पढ़ें.
विज्ञापन एट्रिब्यूशन
आपको विज्ञापन एट्रिब्यूशन दिखाना होगा, ताकि यह पता चल सके कि व्यू एक विज्ञापन है. इस बारे में ज़्यादा जानने के लिए, नीति के दिशा-निर्देश पढ़ें.
क्लिक मैनेज करना
नेटिव विज्ञापन व्यू के ऊपर या उसके अंदर मौजूद किसी भी व्यू पर, कस्टम क्लिक हैंडलर लागू न करें. विज्ञापन व्यू ऐसेट पर होने वाले क्लिक को SDK टूल मैनेज करता है. इसके लिए, आपको ऐसेट व्यू को सही तरीके से पॉप्युलेट और रजिस्टर करना होगा.
क्लिक सुनने के लिए, Google Mobile Ads SDK (बीटा वर्शन) के क्लिक कॉलबैक को लागू करें:
Kotlin
private fun setEventCallback(nativeAd: NativeAd) {
nativeAd.adEventCallback =
object : NativeAdEventCallback {
override fun onAdClicked() {
Log.d(Constant.TAG, "Native ad recorded a click.")
}
}
}
Java
private void setEventCallback(NativeAd nativeAd) {
nativeAd.setAdEventCallback(new NativeAdEventCallback() {
@Override
public void onAdClicked() {
Log.d(Constant.TAG, "Native ad recorded a click.");
}
});
}
ImageScaleType
इमेज दिखाते समय, MediaView
क्लास में ImageScaleType
प्रॉपर्टी होती है. अगर आपको MediaView
में इमेज को स्केल करने का तरीका बदलना है, तो MediaView
के setImageScaleType()
तरीके का इस्तेमाल करके, उससे जुड़ा ImageView.ScaleType
सेट करें:
Kotlin
nativeAdViewBinding.mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
Java
nativeAdViewBinding.mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
MediaContent
MediaContent
क्लास में, नेटिव विज्ञापन के मीडिया कॉन्टेंट से जुड़ा डेटा होता है. इसे MediaView
क्लास का इस्तेमाल करके दिखाया जाता है. जब MediaContent
इंस्टेंस के साथ MediaView
mediaContent
प्रॉपर्टी सेट की जाती है, तब:
अगर कोई वीडियो ऐसेट उपलब्ध है, तो उसे बफ़र किया जाता है और वह
MediaView
में चलने लगती है.hasVideoContent()
देखकर पता लगाया जा सकता है कि कोई वीडियो ऐसेट उपलब्ध है या नहीं.अगर विज्ञापन में वीडियो ऐसेट नहीं है, तो
mainImage
ऐसेट डाउनलोड हो जाती है और उसेMediaView
में रख दिया जाता है.
विज्ञापन को मिटाना
नेटिव विज्ञापन दिखाने के बाद, उसे डिस्ट्रॉय करें. यहां दिए गए उदाहरण में, नेटिव विज्ञापन को हटाने का तरीका बताया गया है:
Kotlin
nativeAd.destroy()
Java
nativeAd.destroy();
अगले चरण
इन विषयों के बारे में जानें:
उदाहरण
Google Mobile Ads SDK (बीटा) के इस्तेमाल के बारे में बताने वाला उदाहरण ऐप्लिकेशन डाउनलोड करें और उसे चलाएं.