Com google android gms ads formats unifiednativeadview

Native advanced

Display a NativeAd

When a native ad loads, the Google Mobile Ads SDK invokes the listener for the corresponding ad format. Your app is then responsible for displaying the ad, though it doesn’t necessarily have to do so immediately. To make displaying system-defined ad formats easier, the SDK offers some useful resources, as described below.

NativeAdView class

For the NativeAd format, there is the corresponding NativeAdView class. This class is a ViewGroup that publishers should use as the root for the NativeAd . A single NativeAdView corresponds to a single native ad. Each view used to display that ad’s assets (the ImageView that displays the screenshot asset, for instance) should be a child of the NativeAdView object.

The view hierarchy for a native ad that uses a LinearLayout to display its asset views might look like this:

Here is an example that creates a NativeAdView and populates it with a NativeAd :

Kotlin

The ad view classes also provide methods used to register the view used for each individual asset, and one to register the NativeAd object itself. Registering the views in this way allows the SDK to automatically handle tasks such as:

  • Recording clicks
  • Recording impressions (when the first pixel is visible on the screen)
  • Displaying the AdChoices overlay for native backfill creatives (currently limited to a select group of publishers)

AdChoices overlay

An AdChoices overlay is added as an ad view by the SDK when a backfill ad is returned. If your app uses native ads backfill, please leave space in your preferred corner of your native ad view for the automatically inserted AdChoices logo. Also, it’s important that the AdChoices overlay be easily seen, so choose background colors and images appropriately. For more information on the overlay’s appearance and function, refer to the programmatic native ads implementation guidelines.

Ad attribution for programmatic native ads

When displaying programmatic native ads, you must display an ad attribution to denote that the view is an advertisement. Learn more in our policy guidelines.

Code example

These are the steps for displaying a native ad:

  1. Create an instance of the NativeAdView class.
  2. For each ad asset to be displayed:
    1. Populate the asset view with the asset in the ad object.
    2. Register the asset view with the ViewGroup class.
  3. Register the MediaView if your native ad layout includes a large media asset.
  4. Register the ad object with the ViewGroup class.

Here is an example function that displays a NativeAd :

Kotlin

Let’s take a look at the individual tasks:

Inflate the layout

Kotlin

In this example, we’re inflating an XML layout that contains views for displaying a native ad and then locating a reference to the NativeAdView . Note that you could also reuse an existing NativeAdView if there’s one in your fragment or activity, or even create an instance dynamically without using a layout file.

Читайте также:  Android studio dialogfragment возврат значения

Populate and register the asset views

This sample code locates the view used to display the headline, sets its text using the string asset provided by the ad object, and registers it with the NativeAdView object:

Kotlin

This process of locating the view, setting its value, and registering it with the ad view class should be repeated for each of the assets provided by the native ad object that the app will display.

Click handling

Clicks on the ad view assets are handled by the SDK as long as you correctly populate and register the asset views, as discussed in the previous section.

Here is an example that uses an ad listener to observe click events:

Kotlin

Register the MediaView

The MediaView is a special View designed to display the main media asset, either video or image.

MediaView can be defined in an XML layout or constructed dynamically. It should be placed within the view hierarchy of a NativeAdView , just like any other asset view. Apps using a MediaView must register it with the NativeAdView like this:

Kotlin

As with all asset views, the media view needs to have its content populated. This is done using the getMediaContent() method to retrieve media content that can be passed to a MediaView . Here is a code snippet setting the media content for the media view:

Kotlin

Setting ImageScaleType

The MediaView class has an ImageScaleType property when displaying images. If you want to change how an image is scaled in the MediaView , set the corresponding ImageView.ScaleType using the setImageScaleType() method of the MediaView .

For example, to fill the MediaView when an image is displayed (ad has no video):

Kotlin

GitHub Sample

Our GitHub repository has the complete implementations for native custom rendering ads written in both Java and Kotlin.

MediaContent

The MediaContent class holds the data related to the media content of the native ad, which is displayed using the MediaView class. When the MediaView mediaContent property is set with a MediaContent instance:

  • If a video asset is available, it’s buffered and starts playing inside the MediaView . You can tell if a video asset is available by checking hasVideoContent() .
  • If the ad does not contain a video asset, the mainImage asset is downloaded and placed inside the MediaView instead.

Note: By default, mainImage is the first downloaded image asset. If setReturnUrlsForImageAssets(true) is used, mainImage is null and you must set the mainImage property to your manually downloaded image. Note that this image will be used only when there is no video asset available.

Register the native ad object

This final step registers the native ad object with the view that’s responsible for displaying it:

Kotlin

Destroy ad

When you are done showing your native ad, you should destroy it so that the ad is properly garbage collected.

Читайте также:  Аэропульт для android приставки

Kotlin

Test native ad code

Direct-sold ads

If you’d like to test out what direct-sold native ads are like, you can make use of this Ad Manager ad unit ID:

It’s configured to serve sample app install and content ads, as well as a custom native ad format with the following assets:

  • Headline (text)
  • MainImage (image)
  • Caption (text)

The template ID for the custom native ad format is 10063170 .

Native backfill ads

To test the behavior of native backfill ads, use this Ad Manager ad unit:

It serves sample app install and content ads that include the AdChoices overlay.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Prepare for SDK v20

There are many breaking changes coming in version 20.0.0. Version 19.7.0 introduced many new APIs, and deprecated or renamed many classes in preparation for version 20.0.0. This guide highlights the major changes brought about in version 19.7.0 as well as what’s coming for version 20.0.0 (early 2021).

Full-screen format API updates

Starting with version 20.0.0, interstitial, rewarded, rewarded interstitial, and App Open ad formats are standardized to follow a consistent API design.

All full-screen format APIs utilize the following principles:

  • A static load method
  • A similar load callback or handler mechanism
  • Reliance on the FullScreenContentCallback class for presentation callbacks

Legacy RewardedVideoAd API removal

The newer RewardedAd API was first introduced back in March 2019 and has been the preferred rewarded API for over 18 months. It has had more enhancements compared to the legacy RewardedVideoAd API, including the ability to load more than one rewarded ad at a time.

The legacy RewardedVideoAd API is removed in SDK version 20.0.0.

Smart banner deprecation in favor of adaptive banner

Smart banner ads are deprecated in favor of adaptive banner ads. Adaptive banners provide superior performance and more flexibility in setting ad width. If you prefer to continue using full-width banners, that can still be done using adaptive banner, as shown in the following code snippet:

Kotlin

Leave application callback removal

The onAdLeftApplication callback for all ad formats has been removed in favor of ProcessLifecycleOwner . Using an OS-level API notifies you whenever users leave your app, regardless of whether or not it is due to an ad interaction.

Note that the onAdLeftApplication callback was never intended to be an ad click handler, and relying on this callback to report clicks did not produce an accurate metric. For example, a click on the AdChoices icon that launched an external browser invoked the callback but did not count as a click.

Class renames

The table below lists specific class names that have changed or been removed. In summary:

  • Most classes related to UnifiedNativeAd have been renamed to NativeAd .
  • The MobileAds.Settings , NativeExpressAdView , NativeAppInstallAd , NativeContentAd , and InstreamAd classes and views have been removed.
  • All classes with the Publisher prefix have been replaced with an AdManager prefix.
  • The InterstitialAd package name has changed.
Читайте также:  Chm чем открыть android
19.5.0 Class 20.0.0 Class
com.google.android.gms.ads. com.google.android.gms.ads.
reward.RewardedVideoAd rewarded.RewardedAd
reward.RewardedVideoAdListener rewarded.RewardedAdLoadCallback and FullScreenContentCallback
reward.RewardItem rewarded.RewardItem
rewarded.RewardedAdCallback OnUserEarnedRewardListener
formats.UnifiedNativeAdView nativead.NativeAdView
formats.UnifiedNativeAd nativead.NativeAd
formats.UnifiedNativeAdAssetNames nativead.NativeAdAssetNames
formats.UnifiedNativeAd.OnUnifiedNativeAdLoadedListener nativead.NativeAd.OnNativeAdLoadedListener
formats.AdChoicesView nativead.AdChoicesView
formats.NativeAd.AdChoicesInfo nativead.NativeAd.AdChoicesInfo
formats.MediaView nativead.MediaView
formats.NativeAdViewHolder nativead.NativeAdViewHolder
formats.NativeAdOptions nativead.NativeAdOptions
formats.NativeCustomTemplateAd nativead.NativeCustomFormatAd
formats.NativeCustomTemplateAd .OnCustomTemplateAdLoadedListener nativead.NativeCustomFormatAd .OnCustomFormatAdLoadedListener
MobileAds.Settings Removed
doubleclick.PublisherAdRequest admanager.AdManagerAdRequest
doubleclick.PublisherAdView admanager.AdManagerAdView
formats.PublisherAdViewOptions formats.AdManagerAdViewOptions
doubleclick.PublisherInterstitialAd admanager.AdManagerInterstitialAd
InterstitialAd interstitial.InterstitialAd
NativeExpressAdView Removed
instream.InstreamAd Removed
mediation.admob.AdMobExtras Removed
Correlator Removed
search.SearchAdRequest Removed
Interface AdRequest.TagForUnderAgeOfConsent Removed
Interface AdRequest.MaxAdContentRating Removed
formats.NativeAppInstallAd native.NativeAd
formats.NativeAppInstallAdView native.NativeAdView
mediation.NativeAppInstallAdMapper mediation.UnifiedNativeAdMapper
formats.NativeContentAd native.NativeAd
formats.NativeContentAdView native.NativeAdView
mediation.NativeContentAdMapper mediation.UnifiedNativeAdMapper

Methods removed/replaced

The table below lists the specific changes in version 20.0.0. In summary:

  • Outdated initialization methods have been removed from the MobileAds class.
  • Some methods from the AdRequest.Builder() class have either been removed or moved to the RequestConfiguration class.
  • RewardedAd and InterstitialAd APIs have been updated to align with all full-screen formats.
  • Correlator features have been removed.
Class v19.5.0 API v20.0.0 API Notes
AdSize getPortraitBanner AdSizeWithWidth() getPortraitAnchored AdaptiveBannerAdSize()
getLandscapeBanner AdSizeWithWidth() getLandscapeAnchored AdaptiveBannerAdSize()
getCurrentOrientation BannerAdSizeWithWidth() getCurrentOrientationAnchored AdaptiveBannerAdSize()
MobileAds initialize(Context, String) MobileAds.initialize(Context, OnInitializationComplete Listener) The app ID is now set in the AndroidManifest.xml.
initialize(Context, String, MobileAds.Settings) initialize(Context, OnInitializationComplete Listener) The Settings class has been deprecated.
getRewardedVideo AdInstance() Removed Use the RewardedAd API instead.
AdListener onAdFailedToLoad(int) onAd FailedToLoad(LoadAdError)
onAdLeftApplication() Removed Full-screen ad formats use FullscreenContentCallback in place of AdListener, and there is no equivalent method on FullscreenContentCallback. The method has been removed from AdListener.
VideoController getAspectRatio() MediaContent.getAspectRatio()
PublisherAdRequest getGender() Removed
getBirthday() Removed
getNetworkExtras() Removed
setManualImpressions Enabled() Removed
updateCorrelator() Removed See Ad content filtering.
PublisherAdRequest.Builder setBirthday() Removed
setGender() Removed
setIsDesignedForFamilies() Removed See guide.
addTestDevice() RequestConfiguration .Builder .setTestDeviceIds() See Enabling test ads.
tagForChildDirectedTreatment() RequestConfiguration .Builder.setTagFor ChildDirectedTreatment() See Ad content filtering.
setTagFor UnderAgeOfConsent() RequestConfiguration .Builder.setTagFor UnderAgeOfConsent()
setMaxAdContentRating() RequestConfiguration .Builder .setMaxAdContentRating()
AdView getMediationAdapter ClassName() ResponseInfo .getMediationAdapter ClassName() A ResponseInfo object is now available via the AdView method getResponseInfo().
NativeAdOptions setImageOrientation() setMediaAspectRatio()
getImageOrientation getMediaAspectRatio()
RewardedAd loadAd(AdRequest, RewardedAdLoadCallback) RewardedAd.load(Context, String, AdRequest, RewardedAdLoadCallback) RewardedAd now uses the same static load approach as other full-screen formats.
loadAd(PublisherAdRequest, RewardedAdLoadCallback) RewardedAd.load(Context, String, AdManagerAdRequest, RewardedAdLoadCallback)
isLoaded() Removed The callback from the static load method provides an ad that is already loaded.
show(Activity, RewardedAdCallback) show(Activity, OnUserEarnedRewardListener) This show method adheres to the broader full-screen format approach.
RewardedAdLoadCallback onRewardedAd FailedToLoad(int) onAd FailedToLoad(LoadAdError)
onRewardedAd FailedToLoad(LoadAdError) onAd FailedToLoad(LoadAdError)
onRewardedAdLoaded() onAdLoaded(RewardedAd)
AppOpenAdLoadCallback onAppOpenAd FailedToLoad(int) onAd FailedToLoad(LoadAdError)
onAppOpenAd FailedToLoad(LoadAdError) onAd FailedToLoad(LoadAdError)
onAppOpenAd Loaded(AppOpenAd) onAdLoaded(AppOpenAd)
RewardedInterstitialAdLoad Callback onRewardedInterstitialAd FailedToLoad(int) onAd FailedToLoad(LoadAdError)
onRewardedInterstitialAd FailedToLoad(LoadAdError) onAd FailedToLoad(LoadAdError)
onRewardedInterstitialAd Loaded(RewardedInterstitialAd) onAd Loaded(RewardedInterstitialAd)
PublisherInterstitialAd new PublisherInterstitialAd() InterstitialAd.load(Context, String, AdRequest, InterstitialAdLoadCallback) InterstitialAd now uses the same static load approach as other full-screen formats.
setAdListener()
DynamicHeightSearchAd Request getNetworkExtras() Removed The NetworkExtras class has been deprecated.
AdLoader forContentAd() Removed
forAppInstallAd() Removed
withCorrelator() Removed
getMediationAdapter ClassName() Removed

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Оцените статью