Implementing AdColony Ad (Banner/Interstitial/Rewarded) SDK in Android

This post is about how to implement AdColony Ads (Banner/Interstitial/Rewarded) SDK in android application with the help of simple Implementing AdColony Ad Demo Application. AdColony allows you to monetize your app by displaying ads.

Creating AdColonyDemo App

In Implementing AdColony Ad Demo Application, we have two buttons that will display Interstitial and Reward video Ads respectively on button click and at the bottom of the app screen, we will display the banner ad.

To create AdColonyDemo App, follow the steps as shown below:

Step 1: Set up AdColony

  • Sign up and log in to your AdColony account. 
  • After creating an account select MONETIZATION -> Setup New App.
  • Now fill details according to your requirements and then finally click on create button.
Implementing AdColony Ad (Banner/Interstitial/Rewarded) SDK in Android

 

Step 2: Creating Ad Zone Id

  • Click on created app. You will see a unique AdColony App UUID is generated (we will need this later).
  •  Click on Setup New Ad Zone to create ad units (Banner/Interstitial/Rewarded).
Creating Banner Ad 
  • To create banner ad, fill details according to your requirement and then finally click on create button.
  • Open the created banner ad. You will see a unique zone id is created (we will need this later). Similarly a unique zone id will generate for each ad (Banner/Interstitial/Rewarded) that you create.
 
Creating Interstitial Ad
  • To create Interstitial ad, fill details according to your requirement and then finally click on create button.
 
Creating Rewarded Ad
  • To create a Rewarded ad, fill details according to your requirement and then finally click on create button.
  • We have created three different ads for AdColonyDemo Application.
 

Step 3: Integrate AdColony ads in app

  • At app-level build.gradle file add below dependencies and sync project.

build.gradle

dependencies {
implementation 'com.adcolony:sdk:4.6.4'
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
}
  • In the manifest file add internet and vibrate permission and inside the application, tag add attribute android:usesCleartextTraffic=”true”. Also, add two AdColony activities inside the application tag, it is requirement for ads.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.c1ctech.adcolonydemo">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AdColonyDemo"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.adcolony.sdk.AdColonyInterstitialActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />
<activity
android:name="com.adcolony.sdk.AdColonyAdViewActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />
</application>

</manifest>
  • In proguard-rules.pro file add below ProGuard Configuration.

proguard-rules.pro

# For communication with AdColony's WebView
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
Creating Layout file

The activity_main.xml file contains a LinearLayout to show banner ad and two buttons on click of which we will show Interstitial and Rewarded video ad.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnInterstitialAd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="showInterstitialAd"
android:padding="15dp"
android:text="Show Interstitial Ad"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1" />

<Button
android:id="@+id/btnRewardVideoAd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="showRewardVideoAd"
android:padding="15dp"
android:text="Show Reward Video Ad"
app:layout_constraintEnd_toEndOf="@+id/btnInterstitialAd"
app:layout_constraintStart_toStartOf="@+id/btnInterstitialAd"
app:layout_constraintTop_toBottomOf="@+id/btnInterstitialAd"
app:layout_constraintVertical_weight="1" />

<LinearLayout
android:id="@+id/colony"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_weight="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • Inside MainActivity.java, add your app id and zone id created for each ad(Banner/Interstitial/Rewarded).
private final static String APP_ID = "ENTER_YOUR_APP_ID";
private final static String BANNER_ZONE_ID = "ENTER_YOUR_BANNER_ZONE_ID";
private final static String INTERSTITIAL_ZONE_ID = "ENTER_YOUR_INTERSTITIAL_ZONE_ID";
private final static String REWARD_ZONE_ID = "ENTER_YOUR_REWARD_ZONE_ID";
public final static String[] AD_UNIT_Zone_Ids = new String[]{BANNER_ZONE_ID, INTERSTITIAL_ZONE_ID, REWARD_ZONE_ID};
  •  initColonySdk(): method which will initialize AdColony SDK.
private void initColonySdk() {
// Construct optional app options object to be sent with configure
// setKeepScreenOn: set a flag on our Activity's window to keep the display from going to sleep.
AdColonyAppOptions appOptions = new AdColonyAppOptions().setKeepScreenOn(true);

// Configure AdColony in your launching Activity's onCreate() method so that cached ads can
// be available as soon as possible.
AdColony.configure(getApplication(), appOptions, APP_ID, AD_UNIT_Zone_Ids);
}

Implementing Banner Ad

  • initBannerAd() : method will request and load banner ad at the bottom of the screen.
  • bannerListener: is defined to write the custom action on a particular ad event.
private void initBannerAd() {
bannerContainer = (LinearLayout) findViewById(R.id.colony);

AdColonyAdViewListener bannerListener = new AdColonyAdViewListener() {

// Code to be executed when an ad request is filled.
// get adColonyAdView object from adcolony Ad Server.
@Override
public void onRequestFilled(AdColonyAdView adColonyAdView) {

//Remove previous ad view if present.
if (bannerContainer.getChildCount() > 0) {
bannerContainer.removeView(bannerAdColony);
}
bannerContainer.addView(adColonyAdView);
bannerAdColony = adColonyAdView;
}

// Code to be executed when an ad request is not filled
// or when an ad is not loaded.
@Override
public void onRequestNotFilled(AdColonyZone zone) {
super.onRequestNotFilled(zone);
}

//Code to be executed when an ad opens
@Override
public void onOpened(AdColonyAdView ad) {
super.onOpened(ad);
}

//Code to be executed when user closed an ad
@Override
public void onClosed(AdColonyAdView ad) {
super.onClosed(ad);
}

// Code to be executed when the user clicks on an ad.
@Override
public void onClicked(AdColonyAdView ad) {
super.onClicked(ad);
}

// called after onAdOpened(), when a user click opens another app
// (such as the Google Play), backgrounding the current app
@Override
public void onLeftApplication(AdColonyAdView ad) {
super.onLeftApplication(ad);
}
};

// Optional Ad specific options to be sent with request
AdColonyAdOptions adOptions = new AdColonyAdOptions();

//Request Ad
AdColony.requestAdView(BANNER_ZONE_ID, bannerListener, AdColonyAdSize.BANNER, adOptions);
}

Implementing Interstitial Ad

  • initInterstitialAd(): method will request and load Interstitial Ad.
  • interstitialListener: is defined to write the custom action on a particular ad event.
private void initInterstitialAd() {

// Set up listener for interstitial ad callbacks.
// Implement only the needed callbacks.
interstitialListener = new AdColonyInterstitialListener() {

// Code to be executed when an ad request is filled.
// get AdColonyInterstitial object from adcolony Ad Server.
@Override
public void onRequestFilled(AdColonyInterstitial adIn) {
// Ad passed back in request filled callback, ad can now be shown
interstitialAdColony = adIn;
isInterstitialLoaded = true;
}

// Code to be executed when an ad request is not filled
@Override
public void onRequestNotFilled(AdColonyZone zone) {
super.onRequestNotFilled(zone);
}

//Code to be executed when an ad opens
@Override
public void onOpened(AdColonyInterstitial ad) {
super.onOpened(ad);
}

//Code to be executed when user closed an ad
@Override
public void onClosed(AdColonyInterstitial ad) {
super.onClosed(ad);
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();

//request new Interstitial Ad on close
AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);
}

// Code to be executed when the user clicks on an ad.
@Override
public void onClicked(AdColonyInterstitial ad) {
super.onClicked(ad);
}

// called after onAdOpened(), when a user click opens another app
// (such as the Google Play), backgrounding the current app
@Override
public void onLeftApplication(AdColonyInterstitial ad) {
super.onLeftApplication(ad);
}

// Code to be executed when an ad expires.
@Override
public void onExpiring(AdColonyInterstitial ad) {
super.onExpiring(ad);
}
};
// Optional Ad specific options to be sent with request
interstitialAdOptions = new AdColonyAdOptions();
AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);
}
  • showInterstitialAd(): method will show Interstitial ad on button click. If ad is not loaded or ad request is not filled from the server then it will show a toast message.
public void showInterstitialAd(View view) {
if (interstitialAdColony != null && isInterstitialLoaded) {
interstitialAdColony.show();
isInterstitialLoaded = false;
} else {
Toast.makeText(getApplicationContext(), "Interstitial Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();
}
}

Implementing Rewarded Video Ad

  • initRewardedAd(): method will request and load Reward Ad.
  • rewardListener: is defined to write the custom action on a particular ad event.
  • AdColony.setRewardListener(): is defined to check the result of the reward video if the reward is watched successfully then we will toast Reward Earned else Toast Reward Cancelled.
private void initRewardedAd() {

//setRewardListener:set the AdColonyRewardListener for global reward callbacks for the app.
AdColony.setRewardListener(new AdColonyRewardListener() {

@Override
public void onReward(AdColonyReward reward) {

if (reward.success()) {
Toast.makeText(getApplicationContext(), "Reward Earned", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Reward Cancelled", Toast.LENGTH_SHORT).show();
}
}
});
// Set up listener for interstitial ad callbacks. You only need to implement the callbacks
// that you care about.
rewardListener = new AdColonyInterstitialListener() {

// Code to be executed when an ad request is filled
// get AdColonyInterstitial Reward object from adcolony Ad Server
@Override
public void onRequestFilled(AdColonyInterstitial adReward) {
// Ad passed back in request filled callback, ad can now be shown
rewardAdColony = adReward;
isRewardLoaded = true;
}

// Code to be executed when an ad request is not filled
@Override
public void onRequestNotFilled(AdColonyZone zone) {
}

//Code to be executed when an ad opens
@Override
public void onOpened(AdColonyInterstitial ad) {
super.onOpened(ad);
}

//Code to be executed when user closed an ad
@Override
public void onClosed(AdColonyInterstitial ad) {
super.onClosed(ad);
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();

//request new reward on close
AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);
}

// Code to be executed when the user clicks on an ad.
@Override
public void onClicked(AdColonyInterstitial ad) {
super.onClicked(ad);
}

// called after onAdOpened(), when a user click opens another app
// (such as the Google Play), backgrounding the current app
@Override
public void onLeftApplication(AdColonyInterstitial ad) {
super.onLeftApplication(ad);
}

// Code to be executed when an ad expires.
@Override
public void onExpiring(AdColonyInterstitial ad) {
super.onExpiring(ad);
}
};
// Ad specific options to be sent with request
rewardAdOptions = new AdColonyAdOptions()
.enableConfirmationDialog(false)
.enableResultsDialog(false);
AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);
}
  • showRewardVideoAd(): method will show reward ad on button click. If ad is not loaded or ad request not filled from the server then it will show a toast message.
public void showRewardVideoAd(View view) {
if (rewardAdColony != null && isRewardLoaded) {
rewardAdColony.show();
isRewardLoaded = false;
} else {
Toast.makeText(getApplicationContext(), "Reward Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();
}
}

Complete Code

The MainActivity.java file contains the following code:

MainActivity.java

package com.c1ctech.adcolonydemo;

import androidx.appcompat.app.AppCompatActivity;
import com.adcolony.sdk.*;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private final static String APP_ID = "ENTER_YOUR_APP_ID";
private final static String BANNER_ZONE_ID = "ENTER_YOUR_BANNER_ZONE_ID";
private final static String INTERSTITIAL_ZONE_ID = "ENTER_YOUR_INTERSTITIAL_ZONE_ID";
private final static String REWARD_ZONE_ID = "ENTER_YOUR_REWARD_ZONE_ID";
public final static String[] AD_UNIT_Zone_Ids = new String[]{BANNER_ZONE_ID, INTERSTITIAL_ZONE_ID, REWARD_ZONE_ID};

private LinearLayout bannerContainer;
private AdColonyAdView bannerAdColony;

private AdColonyInterstitial interstitialAdColony;
private AdColonyInterstitialListener interstitialListener;
private AdColonyAdOptions interstitialAdOptions;

private AdColonyInterstitial rewardAdColony;
private AdColonyInterstitialListener rewardListener;
private AdColonyAdOptions rewardAdOptions;

private static boolean isInterstitialLoaded;
private static boolean isRewardLoaded;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initColonySdk();
initBannerAd();
initInterstitialAd();
initRewardedAd();
}

private void initColonySdk() {
// Construct optional app options object to be sent with configure
// setKeepScreenOn: set a flag on our Activity's window to keep the display from going to sleep.
AdColonyAppOptions appOptions = new AdColonyAppOptions().setKeepScreenOn(true);

// Configure AdColony in your launching Activity's onCreate() method so that cached ads can
// be available as soon as possible.
AdColony.configure(getApplication(), appOptions, APP_ID, AD_UNIT_Zone_Ids);
}

private void initBannerAd() {
bannerContainer = (LinearLayout) findViewById(R.id.colony);

AdColonyAdViewListener bannerListener = new AdColonyAdViewListener() {

// Code to be executed when an ad request is filled
// or when an ad finishes loading.
@Override
public void onRequestFilled(AdColonyAdView adColonyAdView) {

//Remove previous ad view if present.
if (bannerContainer.getChildCount() > 0) {
bannerContainer.removeView(bannerAdColony);
}
bannerContainer.addView(adColonyAdView);
bannerAdColony = adColonyAdView;
}

// Code to be executed when an ad request is not filled
//or when an ad is not loaded.
@Override
public void onRequestNotFilled(AdColonyZone zone) {
super.onRequestNotFilled(zone);
}

//Code to be executed when an ad opens
@Override
public void onOpened(AdColonyAdView ad) {
super.onOpened(ad);
}

//Code to be executed when user closed an ad
@Override
public void onClosed(AdColonyAdView ad) {
super.onClosed(ad);
}

// Code to be executed when the user clicks on an ad.
@Override
public void onClicked(AdColonyAdView ad) {
super.onClicked(ad);
}

// called after onAdOpened(), when a user click opens another app
// (such as the Google Play), backgrounding the current app
@Override
public void onLeftApplication(AdColonyAdView ad) {
super.onLeftApplication(ad);
}
};

// Optional Ad specific options to be sent with request
AdColonyAdOptions adOptions = new AdColonyAdOptions();

//Request Ad
AdColony.requestAdView(BANNER_ZONE_ID, bannerListener, AdColonyAdSize.BANNER, adOptions);
}

private void initInterstitialAd() {

// Set up listener for interstitial ad callbacks.
// Implement only the needed callbacks.
interstitialListener = new AdColonyInterstitialListener() {

// Code to be executed when an ad request is filled.
// get AdColonyInterstitial object from adcolony Ad Server.
@Override
public void onRequestFilled(AdColonyInterstitial adIn) {
// Ad passed back in request filled callback, ad can now be shown
interstitialAdColony = adIn;
isInterstitialLoaded = true;
}

// Code to be executed when an ad request is not filled
@Override
public void onRequestNotFilled(AdColonyZone zone) {
super.onRequestNotFilled(zone);
}

//Code to be executed when an ad opens
@Override
public void onOpened(AdColonyInterstitial ad) {
super.onOpened(ad);
}

//Code to be executed when user closed an ad
@Override
public void onClosed(AdColonyInterstitial ad) {
super.onClosed(ad);
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();

//request new Interstitial Ad on close
AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);
}

// Code to be executed when the user clicks on an ad.
@Override
public void onClicked(AdColonyInterstitial ad) {
super.onClicked(ad);
}

// called after onAdOpened(), when a user click opens another app
// (such as the Google Play), backgrounding the current app
@Override
public void onLeftApplication(AdColonyInterstitial ad) {
super.onLeftApplication(ad);
}

// Code to be executed when an ad expires.
@Override
public void onExpiring(AdColonyInterstitial ad) {
super.onExpiring(ad);
}
};
// Optional Ad specific options to be sent with request
interstitialAdOptions = new AdColonyAdOptions();
AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);
}

private void initRewardedAd() {

//setRewardListener:set the AdColonyRewardListener for global reward callbacks for the app.
AdColony.setRewardListener(new AdColonyRewardListener() {

@Override
public void onReward(AdColonyReward reward) {

if (reward.success()) {
Toast.makeText(getApplicationContext(), "Reward Earned", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Reward Cancelled", Toast.LENGTH_SHORT).show();
}
}
});
// Set up listener for interstitial ad callbacks.
// Implement only the needed callbacks.
rewardListener = new AdColonyInterstitialListener() {

// Code to be executed when an ad request is filled
// get AdColonyInterstitial Reward object from adcolony Ad Server
@Override
public void onRequestFilled(AdColonyInterstitial adReward) {
// Ad passed back in request filled callback, ad can now be shown
rewardAdColony = adReward;
isRewardLoaded = true;
}

// Code to be executed when an ad request is not filled
@Override
public void onRequestNotFilled(AdColonyZone zone) {
}

//Code to be executed when an ad opens
@Override
public void onOpened(AdColonyInterstitial ad) {
super.onOpened(ad);
}

//Code to be executed when user closed an ad
@Override
public void onClosed(AdColonyInterstitial ad) {
super.onClosed(ad);
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();

//request new reward on close
AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);
}

// Code to be executed when the user clicks on an ad.
@Override
public void onClicked(AdColonyInterstitial ad) {
super.onClicked(ad);
}

// called after onAdOpened(), when a user click opens another app
// (such as the Google Play), backgrounding the current app
@Override
public void onLeftApplication(AdColonyInterstitial ad) {
super.onLeftApplication(ad);
}

// Code to be executed when an ad expires.
@Override
public void onExpiring(AdColonyInterstitial ad) {
super.onExpiring(ad);
}
};
// Ad specific options to be sent with request
rewardAdOptions = new AdColonyAdOptions()
.enableConfirmationDialog(false)
.enableResultsDialog(false);
AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);
}

public void showInterstitialAd(View view) {
if (interstitialAdColony != null && isInterstitialLoaded) {
interstitialAdColony.show();
isInterstitialLoaded = false;
} else {
Toast.makeText(getApplicationContext(), "Interstitial Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();
}
}

public void showRewardVideoAd(View view) {
if (rewardAdColony != null && isRewardLoaded) {
rewardAdColony.show();
isRewardLoaded = false;
} else {
Toast.makeText(getApplicationContext(), "Reward Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();
}
}
}

Step 4: Run and test your app

When you run your app it will look like this:

7 COMMENTS

  1. Can I just say what a comfort to find somebody who truly understands what theyre talking about on the net. You certainly understand how to bring a problem to light and make it important. A lot more people really need to look at this and understand this side of the story. I cant believe youre not more popular because you definitely possess the gift.

LEAVE A REPLY

Please enter your comment!
Please enter your name here