Overview

Banner ads are rectangular images that occupy a spot at the top, middle, or bottom within an app's layout. Banner ads refresh automatically at intervals. When a user taps a banner ad, the user is redirected to the advertiser's page in most cases.
This codelab will step you through the process of adding a banner ad and correctly displaying it in your app.

What You Will Create

In this codelab, you will build an empty app and integrate a banner ad into it.
Your app will:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Opening a Project in Android Studio

  1. Start Android Studio.
  2. Start a new project, select Empty Activity, and name the project BannerExample.
  3. Click Finish.

Configuring the HUAWEI Ads SDK Repository and Dependency Package

Configure the Maven repository address in the project-level build.gradle file.

buildscript { repositories { google() jcenter() maven { url 'https://developer.huawei.com/repo/' } } ... } allprojects { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }

Configure the dependency package in the app-level build.gradle file.

dependencies { ... implementation 'com.huawei.hms:ads-lite:13.4.32.300' ... }

Click Sync Now and wait until synchronization is complete.

Configuring Obfuscation Scripts

Before building the APK, configure obfuscation scripts to prevent the HUAWEI Ads SDK from being obfuscated. If obfuscation arises, the HUAWEI Ads SDK may not function properly.

  1. Open the obfuscation script file of your Android project.
  2. Add the following two lines of code to the app/proguard-rules.pro file to exclude the HUAWEI Ads SDK from obfuscation:
    -keep class com.huawei.openalliance.ad.** { *; } -keep class com.huawei.hms.ads.** { *; }

To obtain ads using the HUAWEI Ads SDK, you must first initialize the SDK in your app. This process must be completed during app launch. You do not need to repeat this process when the app is running.
Call HwAds.init() in the onCreate() method of the MainActivity class to initialize the HUAWEI Ads SDK.
MainActivity.java

import com.huawei.hms.ads.HwAds; ... public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the HUAWEI Ads SDK. HwAds.init(this); ... } ... }

The HUAWEI Ads SDK provides an ad view class BannerView to display a banner ad.
The HUAWEI Ads SDK supports the following methods of adding a BannerView class:

Editing the XML layout file:

Add BannerView to the XML 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 the activity_main.xml layout file in the res/layout/ directory.
activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:hwads="http://schemas.android.com/apk/res-auto" android:id="@+id/root_view" android:layout_width="match_parent" android:layout_height="match_parent"> <com.huawei.hms.ads.banner.BannerView android:id="@+id/hw_banner_view" android:layout_width="match_parent" android:layout_height="wrap_content" hwads:adId="testw6vs28auh3" hwads:bannerSize="BANNER_SIZE_360_57"/> </RelativeLayout>

You have added a banner ad view whose size is specified by BANNER_SIZE_360_57 and the test ad slot ID is testw6vs28auh3.

Coding

Instead of creating an XML file, you only need to add BannerView in MainActivity.java by referring to the following code and set the ad slot ID and banner ad size (BannerAdSize) by using the setAdId and setBannerAdSize methods respectively:
MainActivity.java

BannerView bannerView = new BannerView(this); // testw6vs28auh3 indicates a dedicated test ad slot ID. bannerView.setAdId("testw6vs28auh3"); bannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_360_57); // Add BannerView to the layout. RelativeLayout rootView = findViewById(R.id.root_view); rootView.addView(bannerView);

After BannerView is added, you need to call the loadAd() method of the BannerView class in MainActivity to obtain a banner ad.
MainActivity.java

package com.example.bannerexample; import android.os.Bundle; import android.widget.RelativeLayout; import androidx.appcompat.app.AppCompatActivity; import com.huawei.hms.ads.AdParam; import com.huawei.hms.ads.HwAds; import com.huawei.hms.ads.BannerAdSize; import com.huawei.hms.ads.banner.BannerView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the HUAWEI Ads SDK. HwAds.init(this); // Obtain BannerView based on the configuration in layout/ad_fragment.xml. BannerView bottomBannerView = findViewById(R.id.hw_banner_view); AdParam adParam = new AdParam.Builder().build(); bottomBannerView.loadAd(adParam); // Call new BannerView(Context context) to create a BannerView class. BannerView topBannerView = new BannerView(this); topBannerView.setAdId("testw6vs28auh3"); topBannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_360_57); topBannerView.loadAd(adParam); RelativeLayout rootView = findViewById(R.id.root_view); rootView.addView(topBannerView); } }

After running the project, you will see the banner ad in your app.

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following links:

You can click the button below to download the source code.

Download source code

Code copied