Huawei Ads Kit Using Splash Ads

Sanghati Mukherjee
4 min readJul 24, 2020

--

In previous article i.e. HMS Core Ads Kit (Part 1) we learn how integrate reward ads kit in our app. Today we are going to see how to integrate ads in splash screen by using HMS Splash Ads kit.

Splash ads are displayed immediately after an app is launched, even before the home screen of the app is displayed.

It’s time to learn

First we need the latest version of the HMS ads kit. So we need to add HMS Ads kit dependencies in the app gradle file and sync the app.

implementation 'com.huawei.hms:ads-lite:13.4.29.301'

This dependency supports all the ads including splash ads and interstitial ads.

Let’s Code

The layout of the splash screen should be look like this:

Huawei Splash Ads

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashActivity">
<RelativeLayout
android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:background="#fde0e0"
android:layout_alignParentBottom="true"
android:visibility="visible">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:layout_marginBottom="29dp"
android:orientation="vertical">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="2dp"
android:gravity="center"
android:orientation="horizontal">

<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@mipmap/hw_logo3" />

<View
android:layout_width="0.5dp"
android:layout_height="18dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="#000000" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="1"
android:text="HMS Splash Ad "
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Enjoy the Ad"
android:textColor="#8B0000"
android:textSize="14sp" />
</LinearLayout>

</RelativeLayout>
<com.huawei.hms.ads.splash.SplashView
android:id="@+id/splash_ad_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/logo" />
</RelativeLayout>

MAIN CODE

1) A Pause Flag is important to have in the code. Setting the flag parameter to true while existing the app ensure the app home screen is not displayed and setting the flag parameter to false when returning to the splash ad screen from another screen to ensure that the app home screen can be displayed properly.

private boolean hasPaused = false;

2) A method to jump from splash screen to home screen when the ad is complete.

private void goToHomeScreenPage() {
if (!hasPaused) {
hasPaused = true;
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}

3) A loading ad method is required to load the ad using load() method.

private void loadAd() {
AdParam adParam = new AdParam.Builder().build();

SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() {

@Override
public void onAdLoaded() {
// Called when an ad is loaded successfully.
}

@Override

public void onAdFailedToLoad(int errorCode) {

// Called when an ad failed to be loaded. The app home screen is //then displayed.

goToHomeScreenPage();

}

@Override

public void onAdDismissed() {

// Called when the display of an ad is complete. The app home screen // is then displayed.
goToHomeScreenPage();
}

};

int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

// Obtain SplashView.

SplashView splashView = findViewById(R.id.splash_ad_view);

// A default image if the ad is not loaded properly ...

splashView.setSloganResId(R.drawable.default_img);

// Set a logo image.

splashView.setLogoResId(R.mipmap.hw_logo3);

// Set logo description.

splashView.setMediaNameResId(R.string.app_name);

// Set the audio focus preemption policy for a video splash ad.

splashView.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE);

// Load the ad. AD_ID indicates the ad slot ID.

splashView.load(AD_ID, orientation, adParam, splashAdLoadListener);

}

4) When testing rewarded ads, use the dedicated test ad slot ID to obtain test ads. This avoids invalid ad clicks during the test. The test ad slot ID is used only for function commissioning. Before releasing your app, apply for a formal ad slot ID and replace the test ad slot ID with the formal one.

SplashActivtiy.java

public class SplashActivity extends AppCompatActivity {

SplashView splashView;
private static final String AD_ID = "testd7c5cewoj6";
private boolean hasPaused = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);

splashView = findViewById(R.id.splash_ad_view);
loadAd();
}

private void goToHomeScreenPage() {
if (!hasPaused) {
hasPaused = true;
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}

@Override
protected void onStop() {

hasPaused = true;
super.onStop();
}

@Override
protected void onRestart() {
super.onRestart();
hasPaused = false;
goToHomeScreenPage();
}
@Override
protected void onDestroy() {
super.onDestroy();
}

private void loadAd() {

AdParam adParam = new AdParam.Builder().build();
SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() {
@Override
public void onAdLoaded() {
// Called when an ad is loaded successfully.
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Called when an ad failed to be loaded. The app home screen is then displayed.
goToHomeScreenPage();
}
@Override
public void onAdDismissed() {
// Called when the display of an ad is complete. The app home screen is then displayed.
goToHomeScreenPage();
}
};
int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
// Obtain SplashView.
SplashView splashView = findViewById(R.id.splash_ad_view);
// A default image if the ad is not loaded properly ...
splashView.setSloganResId(R.drawable.default_img);
// Set a logo image.
splashView.setLogoResId(R.mipmap.hw_logo3);
// Set logo description.
splashView.setMediaNameResId(R.string.app_name);
// Set the audio focus preemption policy for a video splash ad.
splashView.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE);
// Load the ad. AD_ID indicates the ad slot ID.
splashView.load(AD_ID, orientation, adParam, splashAdLoadListener);
}
}

The Result

ANNOUNCEMENT

Part 3 of this article is coming soon…

For More Information

1) https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/ads-sdk-introduction

2) https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/ads-sdk-guide-reward

HAPPY CODING

--

--

No responses yet