NativeAd को डिसप्ले करना
जब कोई नेटिव विज्ञापन लोड होता है, तो Google Mobile Ads SDK, विज्ञापन के उस फ़ॉर्मैट के लिए लिसनर को शुरू करता है. इसके बाद, विज्ञापन दिखाने की ज़िम्मेदारी आपके ऐप्लिकेशन की होती है. हालांकि, यह ज़रूरी नहीं है कि ऐप्लिकेशन तुरंत विज्ञापन दिखाए. सिस्टम की ओर से तय किए गए विज्ञापन फ़ॉर्मैट को आसानी से दिखाने के लिए, SDK टूल कुछ काम के संसाधन उपलब्ध कराता है. इनके बारे में यहां बताया गया है.
NativeAdView
क्लास को परिभाषित करना
NativeAdView
क्लास को परिभाषित करें. यह क्लास, एक ViewGroup
क्लास है. साथ ही, यह NativeAdView
क्लास के लिए टॉप लेवल का कंटेनर है. हर नेटिव विज्ञापन व्यू में नेटिव विज्ञापन ऐसेट होती हैं. जैसे, MediaView
व्यू एलिमेंट या Title
व्यू एलिमेंट. यह NativeAdView
ऑब्जेक्ट का चाइल्ड होना चाहिए.
एक्सएमएल लेआउट
अपने प्रोजेक्ट में कोई XML NativeAdView
जोड़ें:
<com.google.android.gms.ads.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.gms.ads.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.
}
}
लोड किए गए नेटिव विज्ञापन को मैनेज करना
नेटिव विज्ञापन लोड होने पर, कॉलबैक इवेंट को मैनेज करें, नेटिव विज्ञापन व्यू को बड़ा करें, और उसे व्यू हैरारकी में जोड़ें:
Java
AdLoader.Builder builder = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
// Assumes you have a placeholder FrameLayout in your View layout
// (with ID fl_adplaceholder) where the ad is to be placed.
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
// Assumes that your ad layout is in a file call native_ad_layout.xml
// in the res/layout folder
NativeAdView adView = (NativeAdView) getLayoutInflater()
.inflate(R.layout.native_ad_layout, null);
// This method sets the assets into the ad view.
populateNativeAdView(nativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
Kotlin
val builder = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
.forNativeAd { nativeAd ->
// Assumes you have a placeholder FrameLayout in your View layout
// (with ID fl_adplaceholder) where the ad is to be placed.
val frameLayout: FrameLayout = findViewById(R.id.fl_adplaceholder)
// Assumes that your ad layout is in a file call native_ad_layout.xml
// in the res/layout folder
val adView = layoutInflater
.inflate(R.layout.native_ad_layout, null) as NativeAdView
// This method sets the assets into the ad view.
populateNativeAdView(nativeAd, adView)
frameLayout.removeAllViews()
frameLayout.addView(adView)
}
Jetpack Compose
@Composable
/** Load and display a native ad. */
fun NativeScreen() {
var nativeAd by remember { mutableStateOf<NativeAd?>(null) }
val context = LocalContext.current
var isDisposed by remember { mutableStateOf(false) }
DisposableEffect(Unit) {
// Load the native ad when we launch this screen
loadNativeAd(
context = context,
onAdLoaded = { ad ->
// Handle the native ad being loaded.
if (!isDisposed) {
nativeAd = ad
} else {
// Destroy the native ad if loaded after the screen is disposed.
ad.destroy()
}
},
)
// Destroy the native ad to prevent memory leaks when we dispose of this screen.
onDispose {
isDisposed = true
nativeAd?.destroy()
nativeAd = null
}
}
// Display the native ad view with a user defined template.
nativeAd?.let { adValue -> DisplayNativeAdView(adValue) }
}
fun loadNativeAd(context: Context, onAdLoaded: (NativeAd) -> Unit) {
val adLoader =
AdLoader.Builder(context, NATIVE_AD_UNIT_ID)
.forNativeAd { nativeAd -> onAdLoaded(nativeAd) }
.withAdListener(
object : AdListener() {
override fun onAdFailedToLoad(error: LoadAdError) {
Log.e(TAG, "Native ad failed to load: ${error.message}")
}
override fun onAdLoaded() {
Log.d(TAG, "Native ad was loaded.")
}
override fun onAdImpression() {
Log.d(TAG, "Native ad recorded an impression.")
}
override fun onAdClicked() {
Log.d(TAG, "Native ad was clicked.")
}
}
)
.build()
adLoader.loadAd(AdRequest.Builder().build())
}
ध्यान दें कि किसी नेटिव विज्ञापन की सभी ऐसेट, NativeAdView
लेआउट में रेंडर की जानी चाहिए. Google Mobile Ads SDK, नेटिव ऐसेट को नेटिव विज्ञापन व्यू लेआउट से बाहर रेंडर किए जाने पर चेतावनी लॉग करने की कोशिश करता है.
विज्ञापन व्यू क्लास, हर ऐसेट के लिए इस्तेमाल किए गए व्यू को रजिस्टर करने के तरीके भी उपलब्ध कराती हैं. साथ ही, NativeAd
ऑब्जेक्ट को रजिस्टर करने का तरीका भी उपलब्ध कराती हैं.
इस तरह से व्यू रजिस्टर करने पर, SDK टूल इन जैसे टास्क अपने-आप मैनेज कर सकता है:
- रिकॉर्डिंग पर किए गए क्लिक
- स्क्रीन पर पहला पिक्सल दिखने पर इंप्रेशन रिकॉर्ड करना
- AdChoices ओवरले दिखाना
AdChoices ओवरले
SDK टूल, हर विज्ञापन व्यू में AdChoices ओवरले जोड़ता है. AdChoices लोगो को अपने-आप डालने के लिए, नेटिव विज्ञापन व्यू के पसंदीदा कोने में जगह छोड़ें. यह भी ज़रूरी है कि AdChoices ओवरले आसानी से दिखे. इसलिए, बैकग्राउंड के लिए सही रंग और इमेज चुनें. ओवरले के दिखने के तरीके और इसके फ़ंक्शन के बारे में ज़्यादा जानने के लिए, नेटिव विज्ञापनों के फ़ील्ड के बारे में जानकारी लेख पढ़ें.
विज्ञापन एट्रिब्यूशन
आपको विज्ञापन एट्रिब्यूशन दिखाना होगा, ताकि यह पता चल सके कि व्यू एक विज्ञापन है. इस बारे में ज़्यादा जानने के लिए, नीति के दिशा-निर्देश पढ़ें.
कोड का उदाहरण
नेटिव विज्ञापन दिखाने के लिए, यह तरीका अपनाएं:
NativeAdView
क्लास का एक इंस्टेंस बनाएं.दिखाई जाने वाली हर विज्ञापन ऐसेट के लिए:
- विज्ञापन ऑब्जेक्ट में मौजूद ऐसेट की जानकारी, ऐसेट व्यू में भरें.
- ऐसेट व्यू को
NativeAdView
क्लास के साथ रजिस्टर करें.
अगर आपके नेटिव विज्ञापन लेआउट में बड़ी मीडिया ऐसेट शामिल है, तो
MediaView
रजिस्टर करें.विज्ञापन ऑब्जेक्ट को
NativeAdView
क्लास के साथ रजिस्टर करें.
यहां एक उदाहरण फ़ंक्शन दिया गया है, जो NativeAd
दिखाता है:
Java
private void displayNativeAd(ViewGroup parent, NativeAd ad) {
// Inflate a layout and add it to the parent ViewGroup.
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
NativeAdView adView = (NativeAdView) inflater
.inflate(R.layout.ad_layout_file, parent);
// Locate the view that will hold the headline, set its text, and call the
// NativeAdView's setHeadlineView method to register it.
TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline);
headlineView.setText(ad.getHeadline());
adView.setHeadlineView(headlineView);
// Repeat the process for the other assets in the NativeAd
// using additional view objects (Buttons, ImageViews, etc).
// If the app is using a MediaView, it should be
// instantiated and passed to setMediaView. This view is a little different
// in that the asset is populated automatically, so there's one less step.
MediaView mediaView = (MediaView) adView.findViewById(R.id.ad_media);
adView.setMediaView(mediaView);
// Call the NativeAdView's setNativeAd method to register the
// NativeAdObject.
adView.setNativeAd(ad);
// Ensure that the parent view doesn't already contain an ad view.
parent.removeAllViews();
// Place the AdView into the parent.
parent.addView(adView);
}
Kotlin
fun displayNativeAd(parent: ViewGroup, ad: NativeAd) {
// Inflate a layout and add it to the parent ViewGroup.
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
val adView = inflater.inflate(R.layout.ad_layout_file, parent) as NativeAdView
// Locate the view that will hold the headline, set its text, and use the
// NativeAdView's headlineView property to register it.
val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
headlineView.text = ad.headline
adView.headlineView = headlineView
// Repeat the process for the other assets in the NativeAd using
// additional view objects (Buttons, ImageViews, etc).
val mediaView = adView.findViewById<MediaView>(R.id.ad_media)
adView.mediaView = mediaView
// Call the NativeAdView's setNativeAd method to register the
// NativeAdObject.
adView.setNativeAd(ad)
// Ensure that the parent view doesn't already contain an ad view.
parent.removeAllViews()
// Place the AdView into the parent.
parent.addView(adView)
}
Jetpack Compose
@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
val context = LocalContext.current
Box(modifier = Modifier.padding(8.dp).wrapContentHeight(Alignment.Top)) {
// Call the NativeAdView composable to display the native ad.
NativeAdView {
// Inside the NativeAdView composable, display the native ad assets.
Column(Modifier.align(Alignment.TopStart).wrapContentHeight(Alignment.Top)) {
// Display the ad attribution.
NativeAdAttribution(text = context.getString(R.string.attribution))
Row {
// If available, display the icon asset.
nativeAd.icon?.let { icon ->
NativeAdIconView(Modifier.padding(5.dp)) {
icon.drawable?.toBitmap()?.let { bitmap ->
Image(bitmap = bitmap.asImageBitmap(), "Icon")
}
}
}
Column {
// If available, display the headline asset.
nativeAd.headline?.let {
NativeAdHeadlineView {
Text(text = it, style = MaterialTheme.typography.headlineLarge)
}
}
// If available, display the star rating asset.
nativeAd.starRating?.let {
NativeAdStarRatingView {
Text(text = "Rated $it", style = MaterialTheme.typography.labelMedium)
}
}
}
}
// If available, display the body asset.
nativeAd.body?.let { NativeAdBodyView { Text(text = it) } }
// Display the media asset.
NativeAdMediaView(Modifier.fillMaxWidth().height(500.dp).fillMaxHeight())
Row(Modifier.align(Alignment.End).padding(5.dp)) {
// If available, display the price asset.
nativeAd.price?.let {
NativeAdPriceView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
Text(text = it)
}
}
// If available, display the store asset.
nativeAd.store?.let {
NativeAdStoreView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
Text(text = it)
}
}
// If available, display the call to action asset.
// Note: The Jetpack Compose button implements a click handler which overrides the native
// ad click handler, causing issues. Use the NativeAdButton which does not implement a
// click handler. To handle native ad clicks, use the NativeAd AdListener onAdClicked
// callback.
nativeAd.callToAction?.let { callToAction ->
NativeAdCallToActionView(Modifier.padding(5.dp)) { NativeAdButton(text = callToAction) }
}
}
}
}
}
}
यहाँ अलग-अलग टास्क दिए गए हैं:
लेआउट को बड़ा करना
Java
LayoutInflater inflater = (LayoutInflater) parent.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); NativeAdView adView = (NativeAdView) inflater .inflate(R.layout.ad_layout_file, parent);
Kotlin
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val adView = inflater.inflate(R.layout.ad_layout_file, parent) as NativeAdView
यह कोड, ऐसे एक्सएमएल लेआउट को बड़ा कर रहा है जिसमें नेटिव विज्ञापन दिखाने के लिए व्यू शामिल हैं. इसके बाद, यह
NativeAdView
का रेफ़रंस ढूंढ रहा है. ध्यान दें कि अगर आपके फ़्रैगमेंट या गतिविधि में कोई मौजूदाNativeAdView
है, तो उसका फिर से इस्तेमाल किया जा सकता है. इसके अलावा, लेआउट फ़ाइल का इस्तेमाल किए बिना भी डाइनैमिक तौर पर कोई इंस्टेंस बनाया जा सकता है.ऐसेट व्यू का डेटा जनरेट करना और उन्हें रजिस्टर करना
यह सैंपल कोड, हेडलाइन दिखाने के लिए इस्तेमाल किए गए व्यू का पता लगाता है. साथ ही, विज्ञापन ऑब्जेक्ट से मिली स्ट्रिंग ऐसेट का इस्तेमाल करके, उसके टेक्स्ट को सेट करता है. इसके अलावा, यह उसे
NativeAdView
ऑब्जेक्ट के साथ रजिस्टर करता है:Java
TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline); headlineView.setText(ad.getHeadline()); adView.setHeadlineView(headlineView);
Kotlin
val headlineView = adView.findViewById<TextView>(R.id.ad_headline) headlineView.text = ad.headline adView.headlineView = headlineView
व्यू का पता लगाने, उसकी वैल्यू सेट करने, और उसे विज्ञापन व्यू क्लास के साथ रजिस्टर करने की इस प्रोसेस को, ऐप्लिकेशन में दिखने वाले नेटिव विज्ञापन ऑब्जेक्ट की हर एसेट के लिए दोहराया जाना चाहिए.
क्लिक हैंडल करना
नेटिव विज्ञापन व्यू के ऊपर या उसके अंदर मौजूद किसी भी व्यू पर, कस्टम क्लिक हैंडलर लागू न करें. विज्ञापन व्यू ऐसेट पर होने वाले क्लिक को एसडीके मैनेज करता है. इसके लिए, आपको ऐसेट व्यू को सही तरीके से पॉप्युलेट और रजिस्टर करना होगा. इसके बारे में पिछले सेक्शन में बताया गया है.
क्लिक के बारे में जानने के लिए, Google Mobile Ads SDK के क्लिक कॉलबैक को लागू करें:
Java
AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110") // ... .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(LoadAdError adError) { // Handle the failure by logging. } @Override public void onAdClicked() { // Log the click event or other custom behavior. } }) .build();
Kotlin
val adLoader = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") // ... .withAdListener(object : AdListener() { override fun onAdFailedToLoad(adError: LoadAdError) { // Handle the failure. } override fun onAdClicked() { // Log the click event or other custom behavior. } }) .build()
MediaView को रजिस्टर करना
अगर आपको अपने नेटिव विज्ञापन के लेआउट में मुख्य इमेज ऐसेट शामिल करनी है, तो आपको
ImageView
ऐसेट के बजायMediaView
ऐसेट का इस्तेमाल करना होगा.MediaView
एक खास तरह काView
है. इसे मुख्य मीडिया ऐसेट, यानी कि वीडियो या इमेज दिखाने के लिए डिज़ाइन किया गया है.MediaView
को एक्सएमएल लेआउट में तय किया जा सकता है या डाइनैमिक तौर पर बनाया जा सकता है. इसे किसी अन्य ऐसेट व्यू की तरह,NativeAdView
की व्यू हैरारकी में रखा जाना चाहिए.MediaView
का इस्तेमाल करने वाले ऐप्लिकेशन को इसेNativeAdView
में रजिस्टर करना होगा:Java
// Populate and register the media asset view. nativeAdView.setMediaView(nativeAdBinding.adMedia);
Kotlin
// Populate and register the media asset view. nativeAdView.mediaView = nativeAdBinding.adMedia
ImageScaleType
इमेज दिखाते समय,
MediaView
क्लास मेंImageScaleType
प्रॉपर्टी होती है. अगर आपकोMediaView
में इमेज को स्केल करने का तरीका बदलना है, तोMediaView
केsetImageScaleType()
तरीके का इस्तेमाल करके, उससे जुड़ाImageView.ScaleType
सेट करें:Java
mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
Kotlin
mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
MediaContent
MediaContent
क्लास में, नेटिव विज्ञापन के मीडिया कॉन्टेंट से जुड़ा डेटा होता है. इसेMediaView
क्लास का इस्तेमाल करके दिखाया जाता है. जबMediaContent
इंस्टेंस के साथMediaView
mediaContent
प्रॉपर्टी सेट की जाती है, तब:अगर कोई वीडियो ऐसेट उपलब्ध है, तो उसे बफ़र किया जाता है और वह
MediaView
में चलने लगती है.hasVideoContent()
देखकर पता लगाया जा सकता है कि कोई वीडियो ऐसेट उपलब्ध है या नहीं.अगर विज्ञापन में वीडियो ऐसेट नहीं है, तो
mainImage
ऐसेट डाउनलोड हो जाती है और उसेMediaView
में रख दिया जाता है.
डिफ़ॉल्ट रूप से,
mainImage
पहली डाउनलोड की गई इमेज ऐसेट होती है. अगरsetReturnUrlsForImageAssets(true)
का इस्तेमाल किया जाता है, तोmainImage
null
होता है. साथ ही, आपकोmainImage
प्रॉपर्टी को मैन्युअल तरीके से डाउनलोड की गई इमेज पर सेट करना होगा. ध्यान दें कि इस इमेज का इस्तेमाल सिर्फ़ तब किया जाएगा, जब कोई वीडियो ऐसेट उपलब्ध न हो.नेटिव विज्ञापन ऑब्जेक्ट रजिस्टर करना
इस आखिरी चरण में, नेटिव विज्ञापन ऑब्जेक्ट को उस व्यू के साथ रजिस्टर किया जाता है जो उसे दिखाने के लिए ज़िम्मेदार होता है.
Java
adView.setNativeAd(ad);
Kotlin
adView.setNativeAd(ad)
विज्ञापन को मिटाना
नेटिव विज्ञापन दिखाने के बाद, उसे डिस्ट्रॉय करें. यहां दिए गए उदाहरण में, नेटिव विज्ञापन को हटाने का तरीका बताया गया है:
Java
nativeAd.destroy();
Kotlin
nativeAd.destroy()
GitHub पर मौजूद उदाहरण
नेटिव विज्ञापनों को लागू करने का पूरा उदाहरण:
अगले चरण
इन विषयों के बारे में जानें: