Integrate Native Ads

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

  1. To integrate native ads in your app, add a ViewGroup to your Activity/Fragment, and name it nativeAdContainer.

  2. Design a layout file for native ad.
  3. In your Activity/Fragment file, import NativeAd and add an instance variable for NativeAd.
    KotlinJava
    nativeAd = NativeAd(requireActivity(), placementId).apply {
       adListener = this@NativeAdFragment
       load()
    }
    

Display a Native Ad

  1. 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.
  2. Remove nativeAdContainer child views, and then add native ad layout to nativeAdContainer.
  3. Register all the clickable views.
  4. Call registerViewForInteraction and pass the root view, MediaView, iconView, and clickableViews. Root view and MediaView are required. If there are no clickableViews, MediaView will handle click events.
KotlinJava
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)
           }
       }

Native Ad UI Elements

nativelayout.png

These are the UI elements illustrated in the layout above:

  1. NativeAdLayout: Required. A main view that contains all other native ad components.
  2. ImageView: Optional. A view to display an icon image.
  3. MediaView: Required. A view to display a main image.
  4. TextView: Optional. A label to display the ad title.
  5. TextView: Optional. A label to display app rating.
  6. TextView: Optional. A label to display "Sponsored By" text.
  7. NativeAdOptionsView: Optional. An option view to display privacy icon. Because this is a private class, use the setAdOptionsPosition API as instructed in the third step of the Create and Load a Native Ad section.
  8. TextView: Optional. A label to display ad body text.
  9. 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.

KotlinJava
nativeAd.unregisterView()

Register for Callbacks

To receive notifications for ad events, declare conformance to NativeAdListener protocol and implement callback methods.

KotlinJava
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) {
}

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.

Questions?

Need further assistance, feel free to reach out to us, we’re here to help!

Was this article helpful?