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:
- Call
createAdView(Context, ViewGroup)
to inflate aView
that can show this ad. - Just before the ad is shown to the user, call
prepare(View)
. - Call
renderAdView(View)
with a compatibleView
to render the ad data into the view. - When the ad view is no longer shown to the user, call
clear(View)
. You can later callprepare(View)
again if the ad will be shown to users. - 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 thisView
. Call this when theNativeAd
is no longer seen by a user. If you would like to render a differentNativeAd
into the sameView
, you must call this method first. -
Parameters:
view
: TheView
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 thisNativeAd
. This object is created by aCustomEventNative
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 thetypeMoPubCustomEventNative.MoPubStaticNativeAd
. 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 sameView
and before the ad is shown on screen. This method is commonly used to initialize impression tracking and other state associated with theView
. -
Parameters:
view
: TheView
hosting the native ad.
public void /renderAdView(View view) /+4.0.0
-
Parameters:
view
: TheView
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 March 30, 2021
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.
© 2021 MoPub (a division of Twitter, Inc.)