NativeAd /4.0.0

public class NativeAd

This class represents a native ad instance returned from the MoPub Ad Server, MoPub Exchange, or a mediated native ad network. This class can be used to create and render a View that displays a native ad, tracking impressions and clicks for it. Using MoPubStreamAdPlacer, you can automatically have NativeAds rendered into Views and inserted into your app’s content stream without manipulating this class directly. In general, you should get instances of NativeAd from MoPubNative instances in its MoPubNative.onNativeLoad() callback and should not construct them directly.

When you have a NativeAd instance and wish to show a view:

  1. Call createAdView(Context, ViewGroup) to inflate a View that can show this ad.
  2. Just before the ad is shown to the user, call prepare(View).
  3. Call renderAdView(View) with a compatible View to render the ad data into the view.
  4. When the ad view is no longer shown to the user, call clear(View). You can later call prepare(View) again if the ad will be shown to users.
  5. When the ad will never be shown again, call destroy().

Public Methods

public void /clear(View view) /+4.0.0

  • Description: Clears NativeAd state associated with this View. Call this when the NativeAd is no longer seen by a user. If you would like to render a different NativeAd into the same View, you must call this method first.

  • Parameters:

    • view: The View hosting the native ad.

public View /createAdView(Context context, ViewGroup parent) /+4.0.0

  • Description: Renders the ad view. We strongly recommend that this context be an Activity to preserve the current activity’s theme.

  • Parameters:

    • context: The context, preferably the Activity.
    • parent: An optional parent.

public void /destroy() /+4.0.0

  • Description: Removes existing native ad instance in session. Used when finished showing an ad to free up existing resources.

  • Parameters: None.

public String /getAdUnitId() /+4.0.0

  • Parameters: None.

public BaseNativeAd /getBaseNativeAd() /+4.1.0

  • Description: Returns the BaseNativeAd object for this NativeAd. This object is created by a CustomEventNative subclass after a successful ad request and is specific to the ad source. If it comes from the MoPub Exchange or is a directly-served ad it will be of the typeMoPubCustomEventNative.MoPubStaticNativeAd or MoPubVideoNativeAd. If it is returned by a mediated ad network it may have another type.

  • Parameters: None.

public MoPubAdRenderer /getMoPubAdRenderer() /+4.0.0

  • Parameters: None.

public boolean /isDestroyed() /+4.0.0

  • Parameters: None.

public void /prepare(View view) /+4.0.0

  • Description: Prepares the NativeAd to be seen on screen. You should call this method after calling renderAdView(View) with the same View and before the ad is shown on screen. This method is commonly used to initialize impression tracking and other state associated with the View.

  • Parameters:

    • view: The View hosting the native ad.

public void /renderAdView(View view) /+4.0.0

  • Parameters:

    • view: The View hosting the native ad.

public void /setMoPubNativeEventListener(MoPubNativeEventListener moPubNativeEventListener) /+4.0.0

  • Parameters:

    • moPubNativeEventListener: A listener object for impression and click tracking events.

Last updated July 10, 2020

TWITTER, MOPUB, and the Bird logo are trademarks of Twitter, Inc. or its affiliates. All third party logos and trademarks included are the property of their respective owners.

© 2020 MoPub (a division of Twitter, Inc.)