Native Ads Integration for Android
Native ads are a form of advertising that you can customize to blend in with the other content in your app. This makes ads appear more organic, which leads to a better user experience and higher retention.
Make sure you have correctly integrated the ironSource SDK 7.9.0+ into your application. Integration is outlined here.
To integrate native ads, you need to follow three main steps:
- Load a native ad
- Create a view and bind it
- Destroy the native ad
Step 1. Load a native ad
You’ll need to create a native ad object using LevelPlayNativeAd.Builder class, which allows for custom configuration of the native ad object. We recommend creating a separate class to manage your native ads loading mechanism in a single place.
Make sure to load the native ads after receiving the SDK initialization complete callback.
LevelPlayNativeAd nativeAd = new LevelPlayNativeAd.Builder()
.withPlacementName(placementName)
.withListener(listener)
.build();
// You can add the native ad object to a list of native ads (mNativeAds), which can be used to display the ad when needed
mNativeAds.add(nativeAd);
nativeAd.loadAd();
You can initialize the native ad with the placement name and set the listener for ad callbacks.
Step 2. Implement the Listener
Next, implement the native ad Listener in your code. The ironSource SDK fires several callbacks to inform you of native ad activity. The SDK will notify your Listener of all possible events listed below:
// Invoked each time a native ad was loaded.
@Override
public void onAdLoaded(LevelPlayNativeAd nativeAd, AdInfo adInfo) {
// Your implementation here
}
// Invoked when the native ad loading process has failed.
@Override
public void onAdLoadFailed(LevelPlayNativeAd nativeAd, IronSourceError error) {
// Your implementation here
}
// Invoked each time the first pixel is visible on the screen
@Override
public void onAdImpression(LevelPlayNativeAd nativeAd, AdInfo adInfo) {
// Your implementation here
}
// Invoked when end user clicked on the native ad
@Override
public void onAdClicked(LevelPlayNativeAd nativeAd, AdInfo adInfo) {
// Your implementation here
}
Step 3. Create a view and bind it
You are required to create a view, bind it, and set all relevant information manually before loading the native ad. This includes arranging the different assets such as
title, body text, advertising text, image, media view container, CTA button, and more.
Each native ad is represented by a single NativeAdLayout, and this view is responsible for displaying all the elements of that ad.
Here’s a suggestion for an XML format that can be used as a baseline for displaying your ad:
<com.ironsource.mediationsdk.ads.nativead.NativeAdLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingTop="10dp"
android:contentDescription="com.ironsource.mediationsdk.ads.NativeAdLayout">
<LinearLayout
android:orientation="vertical"
...>
<TextView
android:id="@+id/badge"
.../>
<ImageView
android:id="@+id/ad_app_icon"
...>
<TextView
android:id="@+id/ad_title"
.../>
<Button
android:id="@+id/ad_call_to_action"
.../>
// Other assets such as media view, etc.
...
</LinearLayout>
</com.ironsource.mediationsdk.ads.nativead.NativeAdLayout>
Enhancing ad transparency
Privacy icon – This element should be incorporated into the bottom left corner of your native ad during the design process.
Ad indication – To make sure your users understand that this is an ad, you’re required to mark your native ads as “Ad”. This can be achieved by adding your own indication.
Create a NativeAdLayout and populate it
Inflate the layout
Inflate a layout XML file into a NativeAdLayout object that can be used to display native ad elements within the layout.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
NativeAdLayout nativeAdLayout = (NativeAdLayout) inflater.inflate(R.layout.native_ad, null);
Assets views population and registration
This code sample identifies the view used to display the title, icon, and MediaView. Then, populating the assets, and registering them with the nativeAdLayout object:
// Locate all views to be populated
TextView titleView = nativeAdLayout.findViewById(R.id.ad_title);
ImageView iconView = nativeAdLayout.findViewById(R.id.ad_app_icon);
LevelPlayMediaView mediaView = nativeAdLayout.findViewById(R.id.ad_media);
// Populate the views
String title = nativeAd.getTitle();
if (title != null) {
titleView.setText(title);
}
NativeAdDataInterface.Image icon = nativeAd.getIcon();
if (icon != null) {
if (icon.getDrawable() != null) {
iconView.setImageDrawable(icon.getDrawable());
}
}
// Register the views to be bound
nativeAdLayout.setTitleView(titleView);
nativeAdLayout.setIconView(iconView);
nativeAdLayout.setMediaView(mediaView);
nativeAdLayout.setCallToActionView(ctaView);
// Setting the native ads
nativeLayout.registerNativeAdViews(nativeAd);
For each asset provided by the native ad object that the app will display, repeat the process of locating the view that corresponds to that asset, setting its value, and registering it with the ad view class.
MediaView
The MediaView is a designated container that is intended to display the main media element. It is recommended to use a fixed size for the container.
Step 4. Destroy the Native Ad
To destroy a native ad, call the following method:
nativeAd.destroyAd();
A destroyed native ad can no longer be loaded. If you want to serve it again, you must initiate it once more.
Supported networks
Network | SDK | Adapter |
Google AdMob and Ad Manager | 22.1.0 | 4.3.38 |
Meta | 6.14.0 | 4.3.43 |