Native Mediation

Mediated Native ads are customised views, data is provided from different ad networks and can be displayed to fit the design of any app.

1. Override TMAdListener

Next, you will need to override the TMAdListener class to receive callbacks on mediated native adverts. Create a new file and implement it like so:

public class MediatedNativeListener extends TMAdListener {

    @Override
    public void didLoad(TDMediatedNativeAd ad) {
        // Provides the ad object full of data
    }
}

2. Load the ad

To load a mediated native ad, we recommend you load them once the Tapdaq SDK has initialised.

Go back to the TapdaqInitListener you created in the Quickstart guide, and add a call to load the ad, like so:

public class TapdaqInitListener extends TMInitListener {
    @Override
    public void didInitialise() {
        super.didInitialise();
        // Ads may now be requested
        Tapdaq.getInstance().loadMediatedNativeAd(<CurrentActivity>, "my_tag", new MediatedNativeListener());
    }
}

3. Display the ad

Using the TDMediatedNativeAd provided in the didLoad callback, a layout can now be populated.

NOTE: Original data objects from each of the networks are available in the ad object if you want to interact directly with the network. It can be retrieved with the TDMediatedNativeAd getNativeAd() method

Depending on the ad networks integrated, some fields are required whilst some fields will be null or empty

AdMob AppLovin FAN
Title Required Optional Required
Subtitle N/A N/A Optional
Body Optional Optional Optional
Caption N/A Optional N/A
Media View Optional Optional Optional
Ad View Optional N/A N/A
Call to action Required Optional Required
Ad Choices Optional N/A Optional
Images Optional Optional Optional
Icon Required Optional Optional
Star Rating N/A Optional N/A
Store Optional N/A Optional

3a. Build the layout

Create a custom layout class, e.g. our sample app has a NativeAdLayout class which imports an xml layout resource like so:

public class NativeAdLayout extends RelativeLayout {
    private LayoutInflater mInflater;

    public NativeAdLayout(Context context) {
        super(context);
        mInflater = LayoutInflater.from(context);
        init();
    }

    public NativeAdLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mInflater = LayoutInflater.from(context);
        init();
    }

    public NativeAdLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mInflater = LayoutInflater.from(context);
        init();
    }

    private void init()
    {
        View v = mInflater.inflate(R.layout.nativead_layout, this, true);
    }
}

3b. Populate the layout

For each view element used, populate them with data provided in the ad.

TDMediatedNativeAd ad
TextView mTitleView;
mTitleView.setText(ad.getTitle());

NOTE: When repopulating or replacing an ad, clear all the previous data or use a new view as the new ad may not have all the same data fields as the last.

3c. Trigger impression

For full impression stats, manually call trackImpression when the ad is displayed.

TDMediatedNativeAd ad;
ad.trackImpression(<Context>);

3d. Register clickable views

Some networks handle the click event themselves, for others Tapdaq will handle it - for either case register the view(s) that should be clickable.

TDMediatedNativeAd ad;
ad.registerView(view); //e.g Use Button object as parameter

3e. Displaying image content

Each network handles images differently, some ads will provide a drawable, others just a url. Some ads will also provide multiple images, however its optional to display them.

if (ad.getImages() != null) {
    TDMediatedNativeAdImage image = ad.getImages().get(0);
    if (image.getDrawable() != null) {
        mImageView.setImageDrawable(ad.getImages().get(0).getDrawable());
    } else if (image.getUrl() != null) {
        //Download the image from url
    }
}

3f. Playing video content

Video's will not auto play, use the VideoController object provided to control them.

TDMediatedNativeAd ad;
if (ad.getMediaView() != null) {
    RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,      ViewGroup.LayoutParams.MATCH_PARENT);
    mMediaView.addView(ad.getMediaView(), params);
}
            
if (ad.getVideoController() != null && ad.getVideoController().hasVideoContent()) {
    ad.getVideoController().play();
}

NOTE: Applovin does not provide a MediaView or VideoController, Tapdaq has however implemented a simple one which is automatically used.

4. Destroy the ad

When the ad is being removed from view, call destroy

TDMediatedNativeAd ad;
ad.destroy();

Build and run your application to test the advert displays correctly.

Need help?

If you are having any problems integrating, feel free to contact us on [email protected] and we will be more than happy to help.

What's next?

Now that you have successfully integrated an interstitial into your app, what would you like to do next?