Integrating Banner and Native Ad in Xamarin(Android) using Huawei Ads Kit

Overview

Using Huawei Ads Kit, developers can monetize their app and can make profit. It provides different types of ads like Banner Ad, Native Ad, Rewarded Ad, Interstitial Ad, Splash Ad and Roll Ads. Developers can use these ads on the basis of their requirement.

This application describes how to implement Banner and Native Ad in Xamarin(Android).

Banner Ad: It can be used in application layout at bottom, top and middle. It automatically refreshes on regular intervals.

Native Ad: Developers can use this type of add anywhere in the app’s layout according to the screen design. These ads can be customized as well.

Let us start with the project configuration part:

Step 1: Create an app on App Gallery Connect.

Step 2: Create Android Binding Libraries for Xamarin and use Xamarin plugin version 13.4.29.301.

Step 3: Integrate Ads Libraries for your Xamarin project.

Step 4: Configure Network Permissions.

Step 5: Change your app package name same as AppGallery app’s package name.

a) Right click on your app in Solution Explorer and select properties.

b) Select Android Manifest on lest side menu.

c) Change your Package name as shown in below image.

Step 6: Generate SHA 256 key.

a) Select Build Type as Release.

b) Right click on your app in Solution Explorer and select Archive.

c) If Archive is successful, click on Distribute button as shown in below image.

d) Select Ad Hoc.

e) Click Add Icon.

f) Enter the details in Create Android Keystore and click on Create button.

g) Double click on your created keystore and you will get your SHA 256 key. Save it.

h) Add the SHA 256 key to App Gallery.

Step 7: Sign the .APK file using the keystore.

a) Right click on your app in Solution Explorer and select properties.

b) Select Android Packaging Signing and add the Keystore file path and enter details as shown in image.

Step 8: Now click Build Solution in Build menu.

Banner Ad Implementation Using xml:

Step 1: Put BannerView in the xml layout and set AdId and BannerSize attribute.

Step 2: Load an add using LoadAd() method in BannerView class.

Step 3: Add the listener for Ad events.

Banner Ad Implementation using code:

Step 1: Initialize the BannerView and set the AdId and BannerSize programmatically.

Step 2: Load the add using LoadAd() method.

Step 3: Add the BannerView to the layout.

Native Ad Implementation:

Step 1: Create a xml layout for showing the Native Ad(large image, small image and video).

Step 2: Define all those ad’s id and text in String.xml.

Step 3: Create native_button_rounded_corners_shape.xml and place inside drawable folder.

Step 4: Create native_flag_rounded_corners_shape.xml and place inside drawable folder.

Step 5: Create native_ad_small_layout.xml and place inside layout folder.

Step 6: Create native_ad_video_layout.xml and place inside layout folder.

Step 7: Create NativeAdActivity class and implement NativeAd.INativeAdLoadedListener, IDislikeAdListener.

Step 8: Override OnNativeAdLoaded() and OnAdDisliked() method.

Step 9: Get Ad id on the basis of radio button selected.

Step 10: Load native ad using LoadNativeAd() method.

Step 11: Show native ad inside OnAdLoaded() callback.

Step 12: Load the native ad.

Step 13: Implement the listener if ad load failed.

Step 14: Implement the listener for Native Video Ad.

Now implementation done for Native Ad.

Result

Tips and Tricks

1. Do not forget to sign your .APK file with signing certificate.

2. Please use Xamarin plugin version 13.4.29.201.

Conclusion

This Article explains proper way to implement Banner and Native Ad in Xamarin(Android). With these ads, developer can monetize their app.

Reference

https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides-V1/service-introduction-0000001050178531-V1