[Huawei Developers] Freemind R:
Introduction

Banner Ads are rectangular images that occupy a spot at the top, middle, or bottom within an app's layout. Banner Ads refreshes automatically at intervals. When a user taps a Banner Ad, the user is redirected to the advertiser's page in most cases.

Creating Binding Libraries

To support HUAWEI Ads Kit in Xamarin platform, we need to create Binding libraries. Follow the steps to create Binding libraries:

1. Download Publisher Service libraries that defined in Libraries section of Xamarin Plugin .

2. Download Publisher Service from section Xamarin Plugin.

3. Extract Publisher Service downloaded in Step 2 .

4. Open XAdsLite* solution by double clicking XAdsLite*.sln file in extracted folder of Publisher Service.

5. Each package must place under the related project. Inside the "Solution Explorer", expand each project and repeat the steps below:

Right-click Jars > Add >Existing Item.

Click on the package file you imported.

In the properties window,

i. Set the Build Action as "LibraryProjectZip " if the file type is .aar.

ii. Set the Build Action as "EmbeddedJar" if the file type is .jar.

6. The references of each package should be checked from Check Need References section and should be added to the projects as a reference (Add references from the projects section in reference management.).

7. Build the library

a. From the Visual Studio's toolbar, choose Build > Rebuild Solution.

b. In the output tab, check that all projects are Built successfully.

4. Set all library files' build action to None.

a. Hold CTRL key and select all library files by clicking on them.

b. Right click one of the library files and in the menu click Properties.

c. In the properties window, set Build Action property to None.

5. Add library references to your project

a. Right click to your project’s References section.

b. In the opened menu, click to Add Reference menu item.

c. In opened window, click Browse section. After that click to Browse button.

d. In opened window, navigate to your project's path.

e. Double click to your previously created library folder (example: Libraries).

f. Select all files in this folder and click to Add button.

g. In the Reference Manager window, check that references has been added correctly.

h. Click OK to continue.

Configuring App Information

Configuring the Signing Certificate Fingerprint

1. Sign in to AppGallery Connect and select My Projects.

2. Find your Project from list and click the Project name.

3. In the following page a message window will open requesting to enter a package name.

4. Click on Manually enter the package name button and enter unique package name that you are going to use for your App.

5. Click to Save button.

6. Please take note of this package name that will be used in the following Setting Package Name section.

7. In the App information section of the page, set SHA-256 certificate fingerprint to the SHA-256 fingerprint that is generated from the preceding section of Obtaining the SHA-256 Fingerprint from Signature File.

8. After completing configuration, click

9. Click agconnect-services.json to download the configuration file.

10. Copy the agconnect-services.json file to your project's Assets directory

Setting Package Name

You need to confirm App's package name, before generating apk

1. Right-click on project.

2. Click Properties button.

3. In the window, click on Android Manifest section.

4. Input the package name set during Step 5 of the Configuring the Signing Certificate Fingerprint section to Package name.

5. Choose File > Save Selected Items in main window or press Ctrl+S to save the changes.

Development

1. Add BannerView to theXML layout file, and set the hwads:adId and hwads:bannerSize attributes to the ad slot ID and ad size respectively.

The following sample code shows how to add BannerView to an XML layout file:

<com.huawei.hms.ads.banner.BannerView
android:id="@+id/hw_banner_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
hwads:adId="@string/banner_ad_id"
hwads:bannerSize="BANNER_SIZE_320_50" />

2. Add BannerView to the code, and set the ad slot ID and ad size.

BannerView bannerview = new BannerView(this);
// "testw6vs28auh3" is a dedicated test ad slot ID.
bannerview.AdId = "testw6vs28auh3";
bannerview.BannerAdSize = BannerAdSize.BannerSize32050;
FrameLayout adFrameLayout = FindViewById<FrameLayout>(Resource.Id.ad_frame);
adFrameLayout.AddView(bannerView);.

2. Loading an Ad

After adding BannerView, load an ad using the LoadAd() method in the BannerView class.

// Obtain BannerView based on the configuration in layout/activity_main.xml.
BannerView bannerview = new BannerView(this);
bannerview.AdId = "testw6vs28auh3";
bannerview.BannerAdSize = BannerAdSize.BannerSize32050;
// Create an ad request to load an ad.
AdParam adParam = new AdParam.Builder().Build();
bannerview.LoadAd(adParam);

3. Listening for Ad Events.

You can listen for ad events using the methods in the AdListener class.

bannerview.AdListener = new AdsListener();
private class AdsListener : AdListener
{
BannerActivity activity;
public override void OnAdClicked()
{
// Called when a user taps an ad.
}
public override void OnAdClosed()
{
// Called when an ad is closed.
}
public override void OnAdFailed(int errorCode)
{
// Called when an ad fails to be loaded.
}
public override void OnAdLeave()
{
// Called when a user has left the app.
}
public override void OnAdOpened()
{
// Called when an ad is opened.}public override void OnAdLoaded(){// Called when an ad is loaded successfully.
}
}

Displaying a Banner Ad

• Please ensure version of each .aar file.

• Before build the library project check depedent .dll file in each project Reference folder.

• Please ensure that Ads is enabled or not in AGC console.

Conclusion

This article is very helpful you how to generate .dll files of HMS Banner Ads Kit in visual studio and how to include those generated .dll files into Xamarin.Android project.

Please let me know if you have any queries.

Reference

For more information, refer the following URLs:

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

· https://developer.huawei.com/consum...Library/xamarin-sdk-download-0000001050175494

· https://developer.huawei.com/consum...Examples/xamarin-sample-code-0000001050177525