Skip to main content

Featured

How to integrate adcolony Ad (Banner/Interstitial/Rewarded).

Introduction

In this article we will learn how to implement AdColony Banner/Interstitial/Rewarded Ads in android application. AdColony allows you to monetize your app by displaying ads.


Our Example

In our example we have two button which will display Interstitial and Reward video Ads respectively on button click. At the bottom of app screen we will display banner ad.

 

 


 

 

Requirements:

    Android Project/App in which AdColony ads to be implemented
    AdColony Account

Steps

Follow below steps

1) Create or Sign-in AdColony Account and add application

2) Create Ad units zone id in AdColony account

3) Integrate and code AdColony ads in your app

4) Run and test your application



1) Create or Sign-in AdColony Account and add application

 Sign-in to or sign-up for an AdColony account.

After creating account select MONETIZATION -> Setup New App

 


 Fill details according to your requirements

 


 After filling details click on create.

Now after adding app next step is to create ad unit zone id.


2) Create Ad units zone id in AdColony account

Click on created app and copy AdColony App ID and paste it in a text file (we will need this later).

Then click on Setup New Ad Zone to create ad units (Banner/Interstitial/Rewarded)

Set Setting for Banner Ad according to your requirements.

Show test ads only (for dev or debug)? Is set to yes during development, change it to no during production.

After creating banner ad, open it and copy zone id in text file (we will need this later).

Now set Setting for Interstitial Ad according to your requirements and copy its zone id in text file.

Similarly set Setting for Reward Ad (virtual currency, reward amount) according to your requirements and copy its zone id in text file.

After creating (Banner/Interstitial/Rewarded) ad unit, next step is to integrate adcolony in your app.


3) Integrate and code AdColony ads in your app

At project level build.gradle file add maven { url  “https://adcolony.bintray.com/AdColony”}

At app level build.gradle file add adcolony sdk dependency and sync project

In manifest file add internet and vibrate permission and inside application tag add attribute android:usesCleartextTraffic=”true”. Also add two Adcolony activities inside application tag, it is requirement for ads.

 

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

    <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/AppTheme"
        android:usesCleartextTraffic="true"
        >
        <activity android:name=".MainActivity">
            <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:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:hardwareAccelerated="true"/>
        <activity android:name="com.adcolony.sdk.AdColonyAdViewActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:hardwareAccelerated="true"/>
    </application>

</manifest>


 

In proguard-rules.pro file add below ProGuard Configuration

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

Below is code for layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Display InterstitialAd"
        android:onClick="displayInterstitialAd"
        ></Button>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Display Reward Video Ad"
        android:onClick="displayRewardVideoAd"
        ></Button>
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        ></View>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center"
        android:id="@+id/colony"
        >

    </LinearLayout>

</LinearLayout>

Now Inside MainActivity add app id and (Banner/Interstitial/Rewarded) zone id which was copied during step 2 in a text file. Here we have used our own sample ids replace it with your own ids.


 

private final static String APP_ID = "app7dd43d11ae344f74af";
private final static String BANNER_ZONE_ID = "vz5ccc44c8de0c43f7a8";
private final static String INTERSTITIAL_ZONE_ID = "vz614303d8339f4b95b4";
private final static String REWARD_ZONE_ID = "vze9db182f005c424597";
public final static String[] AD_UNIT_Zone_Ids = new String[] {BANNER_ZONE_ID,INTERSTITIAL_ZONE_ID,REWARD_ZONE_ID};

On app start we have called initColonySdk() method which will initialize AdColony SDK.

private void initColonySdk(){
        // Construct optional app options object to be sent with configure
        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);
    }

initBannerAd() method will request and load banner ad at the bottom of screen. bannerListener is defined to write your custom action on particular ad event. On onRequestFilled() method we will get adColonyAdView object from adcolony Ad Server.

private void initBannerAd() {
        bannerConatiner = (LinearLayout)findViewById(R.id.colony);
  
        AdColonyAdViewListener bannerListener = new AdColonyAdViewListener() {
            @Override
            public void onRequestFilled(AdColonyAdView adColonyAdView) {
                //Remove previous ad view if present.
                if (bannerConatiner.getChildCount() > 0) {
                    bannerConatiner.removeView(bannerAdColony);
                }
                bannerConatiner.addView(adColonyAdView);
                bannerAdColony = adColonyAdView;
            }
            @Override
            public void onRequestNotFilled(AdColonyZone zone) {
                super.onRequestNotFilled(zone);
            }
            @Override
            public void onOpened(AdColonyAdView ad) {
                super.onOpened(ad);
            }
            @Override
            public void onClosed(AdColonyAdView ad) {
                super.onClosed(ad);
            }
            @Override
            public void onClicked(AdColonyAdView ad) {
                super.onClicked(ad);
            }
            @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);
    }

In initInterstitialAd() method will request and load Interstitial Ad. interstitialListener is defined to write your custom action on particular ad event. On onRequestFilled() method we will get AdColonyInterstitial object from adcolony Ad Server. On onClosed() method we have again requested next Interstitial ad.

private void initInterstitialAd() {
        // Set up listener for interstitial ad callbacks. You only need to implement the callbacks
        // that you care about. The only required callback is onRequestFilled, as this is the only
        // way to get an ad object.
        interstitialListener = new AdColonyInterstitialListener() {
            @Override
            public void onRequestFilled(AdColonyInterstitial adIn) {
                // Ad passed back in request filled callback, ad can now be shown
                interstitialAdColony = adIn;
                isInterstitialLoaded=true;
            }
            @Override
            public void onRequestNotFilled(AdColonyZone zone) {
                super.onRequestNotFilled(zone);
            }
            @Override
            public void onOpened(AdColonyInterstitial ad) {
                super.onOpened(ad);
            }
            @Override
            public void onClosed(AdColonyInterstitial ad) {
                super.onClosed(ad);
                //request new Interstitial Ad on close
                AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);
            }
            @Override
            public void onClicked(AdColonyInterstitial ad) {
                super.onClicked(ad);
            }
            @Override
            public void onLeftApplication(AdColonyInterstitial ad) {
                super.onLeftApplication(ad);
            }
            @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);
    }

displayInterstitialAd() method will display Interstitial ad on button click. If ad is not loaded or ad request not filled from server then it will display toast message.

public void displayInterstitialAd(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();
        }
    }

In initRewardedAd () method will request and load Reward Ad. rewardListener is defined to write your custom action on particular ad event. On onRequestFilled() method we will get AdColonyInterstitial Reward object from adcolony Ad Server. On onClosed() method we have again requested next Reward  ad. AdColony.setRewardListener() is defined to check result of reward video if reward is watched successfully then we will toast Reward Earned else Toast Reward Cancelled

private void initRewardedAd() {
        // Create and set a reward listener
        AdColony.setRewardListener(new AdColonyRewardListener() {
            @Override
            public void onReward(AdColonyReward reward) {
                // Query reward object for info here
                //here reward vid is seen by user
                //you can use this listener inside activity also
                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. The only required callback is onRequestFilled, as this is the only
        // way to get an ad object.
        rewardListener = new AdColonyInterstitialListener() {
            @Override
            public void onRequestFilled(AdColonyInterstitial adReward) {
                // Ad passed back in request filled callback, ad can now be shown
                rewardAdColony = adReward;
                isRewardLoaded=true;
            }
            @Override
            public void onRequestNotFilled(AdColonyZone zone) {
            }
            @Override
            public void onOpened(AdColonyInterstitial ad) {
                super.onOpened(ad);
            }
            @Override
            public void onClosed(AdColonyInterstitial ad) {
                super.onClosed(ad);
                //request new reward on close
                AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);
            }
            @Override
            public void onClicked(AdColonyInterstitial ad) {
                super.onClicked(ad);
            }
            @Override
            public void onLeftApplication(AdColonyInterstitial ad) {
                super.onLeftApplication(ad);
            }
            @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);
    }

displayRewardVideoAd() method will show reward ad on button click. If ad is not loaded or ad request not filled from server then it will display toast message.

public void displayRewardVideoAd(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();
        }
    }

Or You can download the source code in this link below:

Adcolony Ad Source code

 You can also download the adcolony lib below this link:

Adcolony Ad lib

Comments

Popular Posts