To integrate any ad format, you must first have completed the instructions in the basic integration article.
Native ads are composed of assets that are presented to users via UI components native to the platform. They’re displayed using the same classes you already use in your storyboards, and can be formatted to match your app's visual design. When a native ad loads, your app receives an ad object containing its assets, and your app (rather than the SDK) is then responsible for displaying them. This differs from other ad formats, which don't allow you to customize the appearance of the ad.
Broadly speaking, there are two parts to successfully implementing native ads: loading an ad via the SDK and displaying the ad content in your app.
Create and Load a Native Ad
-
To integrate native ads in your app, add a
ViewGroup
to yourActivity/Fragment
, and name itnativeAdContainer
. - Design a layout file for native ad.
- In your
Activity/Fragment
file, importNativeAd
and add an instance variable forNativeAd
.nativeAd = NativeAd(requireActivity(), placementId).apply { adListener = this@NativeAdFragment load() }
nativeAd = new NativeAd(requireActivity(), placementId); nativeAd.setAdListener(this); nativeAd.load(null);
Display a Native Ad
- To display a native ad in your application, fill all the elements in the layout file; these usually include button text, icon, main image, etc. Refer to Native Ad UI Elements below.
- Remove
nativeAdContainer
child views, and then add native ad layout tonativeAdContainer
. - Register all the clickable views.
- Call
registerViewForInteraction
and pass the root view,MediaView
,iconView
, andclickableViews
. Root view andMediaView
are required. If there are noclickableViews
,MediaView
will handle click events.
nativeAd?.let { nativeAd -> val layout = LayoutNativeAdBinding.inflate(LayoutInflater.from(requireContext())) with(layout) { binding.adContainer.removeAllViews() val clickableViews = mutableListOf(imgAdIcon, pnlVideoAd, btnAdCta) lbAdTitle.text = nativeAd.getAdTitle() lbAdBody.text = nativeAd.getAdBodyText() lbAdRating.text = String.format("Rating: %s", nativeAd.getAdStarRating()) lbAdSponsor.text = nativeAd.getAdSponsoredText() btnAdCta.text = nativeAd.getAdCallToActionText() btnAdCta.isVisible = nativeAd.hasCallToAction() nativeAd.registerViewForInteraction( root, pnlVideoAd, imgAdIcon, clickableViews ) binding.apply { nativeAdContainer.show() } nativeAdContainer.addView(root) } }
List<View> clickableViews = new ArrayList<>(); clickableViews.add(layout.imgAdIcon); clickableViews.add(layout.pnlVideoAd); clickableViews.add(layout.btnAdCta); nativeAd.registerViewForInteraction(layout.getRoot(), layout.pnlVideoAd, layout.imgAdIcon, clickableViews); layout.lbAdTitle.setText(nativeAd.getAdTitle()); layout.lbAdBody.setText(nativeAd.getAdBodyText()); layout.lbAdRating.setText(String.format("Rating: %s", nativeAd.getAdStarRating())); layout.lbAdSponsor.setText(nativeAd.getAdSponsoredText()); layout.btnAdCta.setText(nativeAd.getAdCallToActionText()); layout.btnAdCta.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.GONE); nativeAdContainer.addView(layout.getRoot()); nativeAdContainer.setVisibility(View.VISIBLE);
Native Ad UI Elements
These are the UI elements illustrated in the layout above:
-
NativeAdLayout
: Required. A main view that contains all other native ad components. -
ImageView
: Optional. A view to display an icon image. -
MediaView
: Required. A view to display a main image. -
TextView
: Optional. A label to display the ad title. -
TextView
: Optional. A label to display app rating. -
TextView
: Optional. A label to display "Sponsored By" text. -
NativeAdOptionsView
: Optional. An option view to display privacy icon. Because this is a private class, use thesetAdOptionsPosition
API as instructed in the third step of the Create and Load a Native Ad section. -
TextView
: Optional. A label to display ad body text. -
Button
: Optional. A button to display Call to Action (CTA) text.
You can place the view anywhere on the screen. This container is a Layout. You can place this Layout anywhere on the screen.
Destroy a Native Ad
In cases where you reuse the view to show different ads over time, make sure to call unregisterView()
before registering the same view with a different instance of NativeAd
.
nativeAd.unregisterView()
nativeAd.unregisterView();
Register for Callbacks
To receive notifications for ad events, declare conformance to NativeAdListener
protocol and implement callback methods.
override fun onAdLoaded(baseAd: BaseAd) { Log.d(TAG, "Creative id:" + baseAd.creativeId) } override fun onAdStart(baseAd: BaseAd) { } override fun onAdImpression(baseAd: BaseAd) { } override fun onAdEnd(baseAd: BaseAd) { } override fun onAdClicked(baseAd: BaseAd) { } override fun onAdLeftApplication(baseAd: BaseAd) { } override fun onAdFailedToLoad(baseAd: BaseAd, adError: VungleError) { } override fun onAdFailedToPlay(baseAd: BaseAd, adError: VungleError) { }
@Override public void onAdClicked(@NonNull BaseAd baseAd) { } @Override public void onAdEnd(@NonNull BaseAd baseAd) { } @Override public void onAdFailedToLoad(@NonNull BaseAd baseAd, @NonNull VungleError vungleError) { } @Override public void onAdFailedToPlay(@NonNull BaseAd baseAd, @NonNull VungleError vungleError) { } @Override public void onAdImpression(@NonNull BaseAd baseAd) { } @Override public void onAdLeftApplication(@NonNull BaseAd baseAd) { } @Override public void onAdLoaded(@NonNull BaseAd baseAd) { Log.d(TAG, "Creative id:" + baseAd.getCreativeId()); } @Override public void onAdStart(@NonNull BaseAd baseAd) { } ;
Test a Native Ad
You can test a native ad in one of two ways: by setting your app status to Test Mode so that Vungle can deliver test ads to your app, or by adding a test device to your app so that Vungle can deliver test ads specifically to the test device. Follow the instructions in Test Your Integration: Test Mode and Test Devices.