FORUMS

Ad Kits: Adding a Native Ad

1,030 posts
Thanks Meter: 1,109
 
By XDARoni, XDA Community Manager on 5th June 2020, 03:21 PM
Post Reply Email Thread
Adding a Native Ad
Load native ads through the NativeAdLoader class.

Step 1: Building an AdLoader
The NativeAdLoader class provides the NativeAdLoader.Builder class for setting the ad slot ID, customizing attributes, and building the NativeAdLoader object. The sample code is as follows:
Code:
// "testy63txaom86" is a dedicated test ad slot ID.
NativeAdLoader.Builder builder = new NativeAdLoader.Builder(this, "testy63txaom86");
builder.setNativeAdLoadedListener(new NativeAd.NativeAdLoadedListener() {
    @Override
    public void onNativeAdLoaded(NativeAd nativeAd) {
        // Call this method when an ad is successfully loaded.
        ...
    }
}).setAdListener(new AdListener() {
    @Override
    public void onAdFailed(int errorCode) {
        // Called when an ad fails to be loaded.
        ...
    }
});


NativeAdLoader nativeAdLoader = builder.build();
If an ad is loaded successfully, the SDK calls the onNativeAdLoaded() method of NativeAd.NativeAdLoadedListener to return the NativeAd object.

If an ad fails to be loaded, the SDK calls the onAdFailed() method of AdListener.

Step 2: Loading an Ad
NativeAdLoader provides the loadAd() and loadAds() methods for ad loading.
  • loadAd() for loading a single native ad
AdParam is the only parameter. The sample code is as follows:
Code:
nativeAdLoader.loadAd(new AdParam.Builder().build());
  • After loadAd() is called, the SDK calls back the added listener method.
AdParam is the only parameter. The sample code is as follows:
  • nativeAdLoader.loadAd(new AdParam.Builder().build());
After loadAds() is called, the listener makes multiple callbacks based on the number of returned ads. An app requesting multiple ads can use the AdLoader.isLoading() method in the callback code to determine whether ad loading is complete. The sample code is as follows:
Code:
// "testy63txaom86" is a dedicated test ad slot ID.
NativeAdLoader.Builder builder = new NativeAdLoader.Builder(this, "testy63txaom86");
NativeAdLoader nativeAdLoader = builder.setNativeAdLoadedListener(new NativeAd.NativeAdLoadedListener() {
    @Override
    public void onNativeAdLoaded(NativeAd nativeAd) {
        if (adLoader.isLoading()) {
            // nativeAdLoader is still loading ads.
            ...
        } else {
            // nativeAdLoader has finished loading ads.
            ...
        }
    }
}).build();
nativeAdLoader.loadAds(new AdParam.Builder().build(), 5);
Step 3: Displaying an Ad
The "Why this ad" icon and an ad identifier must be displayed in the corner of each ad view.

The procedure for displaying a native ad is as follows:

The following is a sample view hierarchy for a native ad that uses RelativeLayout to display its asset view:
Code:
<com.huawei.hms.ads.nativead.NativeView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ... >
    <RelativeLayout
        ... >
        <com.huawei.hms.ads.nativead.MediaView
            android:id="@+id/ad_media"
            ... />
        <RelativeLayout
            ... >
            <TextView
                android:id="@+id/ad_title"
                ...  />
            // Other assets.
            ...
        </RelativeLayout>
        // Other assets.
        ...
    </RelativeLayout>
</com.huawei.hms.ads.nativead.NativeView>
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes