Native Ads list integration
The Instal SDK provides a custom class, InstalAdAdapter, that can be used to display native ads in a ListView.
Prerequisites
Before integrating native ads in your app, you’ll need to go through the steps in our Getting Started Guide and integrate the SDK into your project.
In order to display a native ads, you need to create a "Mobile Native List" AdUnit. You can create a new one here.
Download and display a native ad
In the Activity or in the Fragment in which you want to show the native ad, declare a InstalAdAdapter instance variable:
private InstalAdAdapter instalAdAdapter;
In your Activity’s (or Fragment) onCreate()
method, instantiate the InstalAdAdapter using the context, the origial adapter, the AdStrategy and one or more AdViewHolderFactory:
instalAdAdapter = new InstalAdAdapter(
this,
adapter,
new IntervalAdStrategy(2, 5),
new InstalAdViewHolderFactory(R.layout.your_layout_id, "YOUR_ADUNIT_ID", DemoAdViewHolder.class)
);
You can use an instance of InstalAdViewHolderFactory
to specify an ad unit and the view holder class used to create and populate the ad layout.
In case you want to use multiple ad units or multiple layouts you can use more than one InstalAdViewHolderFactory
paramter.
Lastly, remember to call destroy()
in your Activity or Fragment onDestroy
method.
@Override protected void onDestroy() {
super.onDestroy();
instalAdAdapter.destroy();
}
A complete example is available in sample app.
Ad strategy
The third parameter of InstalAdAdapter
constructor is an object of type AdStrategy
that defines where the ads are displayed in the list.
This interface defines two methods:
public interface AdStrategy {
public abstract int getAdUnitIndex(BaseAdapter wrappedAdapter, int position);
public abstract int getTotalAdViews(BaseAdapter wrappedAdapter);
}
An implementation is already available in the sdk, you can create a new instance of IntervalAdStrategy
using the constructor with two parameters:
- index of the first ad
- interval between two ads
Ad view holder
InstalAdAdapter
manages automatically native ad download, you can define how the layout is populated using a view holder.
View holder is a common pattern used in ListView, more info are available here.
You can define a view holder extending class InstalAdViewHolder and defining a constructor with a View parameter and three methods:
- onStartLoading: invoked every time the view is displayed in the ListView before the loading of the native ad;
- onLoad: invoked when a native ad is ready to be displayed;
- onFail: invoked after a download error.
The last two methods have the same parameters and the same logic of the methods used for native ads.
A complete example is available in sample app.