How to use HUAWEI Roll Ads?
In this article, we will talk about how to show Huawei roll ads in a native android project.
What is Roll Ads?
Roll ads are displayed as short videos or images, before, during, or after the video content is played.
Useful Links Before Start
Check this official roll ads guide to learn the details about the roll ads.
Check this GitHub page for the Ads Demo project.
Preparations Before Coding
Creating an Ad unit id
First things first, We need to create our ad units using Huawei Ads Publisher Service. Check this link to start.
This medium post also might help you for creating a Huawei Ad slot id.
Preparations in Android Studio
Hardware Requirements
A Huawei phone running Android
Software Requirements
- Android Studio
- JDK 1.7 or later
- minSdkVersion: 19
- Gradle version: 4.1 or later
- HMS Core (APK) 5.0.3.300 or later
Integrate The HUAWEI Ads SDK
In your project-level build.gradle, include Huawei’s Maven repository.
buildscript {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}allprojects {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
}
In the app-level build.gradle, include Huawei Ads dependency.
implementation 'com.huawei.hms:ads-lite:13.4.40.302'
Demo Application-Roll Ads
Let’s start with the layout code for the roll ads. We will use Video View for our video content.
The structure of this layout is simple, We have a video View that shows the video content, and there is an invisible layout named ‘instream_ad_container’
This invisible layout contains the elements for the ad. The output of the layout is like this :
Note: Check the Github page of the Demo project if you need drawables or icons.
Let’s continue with our java code.
This is our demo code using Video view. I used ‘startCountDown()’ method to show the ad after 5 seconds. You can implement different algorithm to trigger the advertisement.
We updated ‘currentTime’ variable when the ad is triggered. If the user skips or finishes watching the ad content, the video will resume from the current time.
Note: Enter your ad unit id that is created from the publisher console in ‘configAdLoader()’ method.
Take Care until next time :)