<p>This post is about how to implement AdColony Ads (Banner/Interstitial/Rewarded) SDK in android application with the help of simple <span style="color: #008000;"><strong>AdColonyDemo</strong></span> Application. AdColony allows you to monetize your app by displaying ads.</p> 
 
 
 
<div class="wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> 
<div class="wp-block-button"><a class="wp-block-button__link has-white-color has-text-color has-background" style="background-color: #03136f;" href="https://github.com/arunk7839/AndroidAdColonyExp"><strong>DOWNLOAD CODE</strong></a></div> 
</div> 
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="v7CWUIMA1Zc" title="Implementing AdColony Ads (Banner/Interstitial/Rewarded) SDK in Android"><a placeholder href="https://youtu.be/v7CWUIMA1Zc"><img src="https://i.ytimg.com/vi/v7CWUIMA1Zc/hqdefault.jpg" layout="fill" object-fit="cover" alt="Implementing AdColony Ads (Banner/Interstitial/Rewarded) SDK in Android"></a></amp-youtube></p> 
<h3><strong><span style="color: #000080;">Creating AdColonyDemo App</span></strong></h3> 
<p>In <span style="color: #0000ff;"><strong>AdColonyDemo</strong></span> 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.</p> 
<p>To create AdColonyDemo App, follow the steps as shown below:</p> 
<h4 id="step_1_set_up_adcolony" role="presentation" data-text="Step 1: Set up AdColony"><strong><span class="devsite-heading" style="color: #000080;" role="heading" aria-level="2">Step 1: Set up AdColony</span></strong></h4> 
<ul> 
<li><a href="https://clients.adcolony.com/signup"><strong><span style="color: #008000;">Sign up</span></strong></a> and <a href="https://clients.adcolony.com/login"><span style="color: #008000;"><strong>log in</strong></span></a> to your AdColony account. </li> 
<li>After creating an account select <span style="color: #0000ff;"><strong>MONETIZATION ->; Setup New App</strong></span>.</li> 
<li>Now fill details according to your requirements and then finally click on <span style="color: #0000ff;"><strong>create </strong></span>button.</li> 
</ul> 
<p><img class="alignnone wp-image-2844 size-full" src="https://c1ctech.com/wp-content/uploads/2021/10/img1-e1634661292867.png" alt="" width="697" height="461" /></p> 
<p><img class="alignnone wp-image-2845 size-full" src="https://c1ctech.com/wp-content/uploads/2021/10/imgAdskip-e1634661258170.png" alt="" width="852" height="125" /></p> 
<p><img class="alignnone size-large wp-image-2848" src="https://c1ctech.com/wp-content/uploads/2021/10/CoppaAct-1024x149.png" alt="" width="1024" height="149" /></p> 
<p><img class="alignnone wp-image-2846 size-large" src="https://c1ctech.com/wp-content/uploads/2021/10/Adaudiosett-e1634661218935-1024x258.png" alt="" width="1024" height="258" /></p> 
<p><img class="alignnone wp-image-2847 size-large" src="https://c1ctech.com/wp-content/uploads/2021/10/imgAds-e1634661334949-1024x466.png" alt="" width="1024" height="466" /></p> 
<h4 role="presentation" data-text="Step 1: Set up AdColony"> </h4> 
<h4 role="presentation" data-text="Step 1: Set up AdColony"><strong><span class="devsite-heading" style="color: #000080;" role="heading" aria-level="2">Step 2: </span></strong><span style="color: #000080;"><strong>Creating Ad Zone Id</strong></span></h4> 
<ul> 
<li>Click on created app. You will see a unique <span style="color: #008000;"><strong>AdColony App UUID</strong></span> is generated (we will need this later).</li> 
</ul> 
<p><img class="alignnone size-full wp-image-2849" src="https://c1ctech.com/wp-content/uploads/2021/10/AdColonyAppCreatednew.png" alt="" width="491" height="463" /></p> 
<ul> 
<li> Click on<span style="color: #0000ff;"><strong> Setup New Ad Zone</strong></span> to create ad units (Banner/Interstitial/Rewarded).</li> 
</ul> 
<h5><span style="color: #000080;"><strong>Creating Banner Ad </strong></span></h5> 
<ul> 
<li>To create banner ad, fill details according to your requirement and then finally click on <span style="color: #0000ff;"><strong>create </strong></span>button.</li> 
</ul> 
<p><img class="alignnone size-full wp-image-2850" src="https://c1ctech.com/wp-content/uploads/2021/10/AdZone.png" alt="" width="680" height="515" /></p> 
<p><img class="alignnone wp-image-2851 size-large" src="https://c1ctech.com/wp-content/uploads/2021/10/next-e1634661961426-1024x609.png" alt="" width="1024" height="609" /></p> 
<p><img class="alignnone wp-image-2852 size-full" src="https://c1ctech.com/wp-content/uploads/2021/10/AdSkipping-e1634661894966.png" alt="" width="659" height="474" /></p> 
<ul> 
<li>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.</li> 
</ul> 
<p><img class="alignnone size-full wp-image-2855" src="https://c1ctech.com/wp-content/uploads/2021/10/bannerAdZoneId-1.png" alt="" width="555" height="570" /></p> 
<h5> </h5> 
<h5><span style="color: #000080;"><strong>Creating Interstitial Ad </strong></span></h5> 
<ul> 
<li>To create Interstitial ad, fill details according to your requirement and then finally click on <span style="color: #0000ff;"><strong>create </strong></span>button.</li> 
</ul> 
<p><img class="alignnone size-full wp-image-2856" src="https://c1ctech.com/wp-content/uploads/2021/10/interstitialAd1.png" alt="" width="650" height="538" /></p> 
<p><img class="alignnone size-full wp-image-2857" src="https://c1ctech.com/wp-content/uploads/2021/10/interAd2.png" alt="" width="1024" height="381" /></p> 
<p><img class="alignnone size-large wp-image-2858" src="https://c1ctech.com/wp-content/uploads/2021/10/interAd3-1024x510.png" alt="" width="1024" height="510" /></p> 
<p><img class="alignnone size-full wp-image-2859" src="https://c1ctech.com/wp-content/uploads/2021/10/interAd4.png" alt="" width="683" height="279" /></p> 
<h5> </h5> 
<h5><span style="color: #003366;"><strong>Creating Rewarded Ad </strong></span></h5> 
<ul> 
<li>To create a Rewarded ad, fill details according to your requirement and then finally click on <span style="color: #0000ff;"><strong>create </strong></span>button.</li> 
</ul> 
<p><img class="alignnone size-full wp-image-2861" src="https://c1ctech.com/wp-content/uploads/2021/10/R1.png" alt="" width="666" height="516" /></p> 
<p><img class="alignnone size-large wp-image-2862" src="https://c1ctech.com/wp-content/uploads/2021/10/R2-1024x742.png" alt="" width="1024" height="742" /></p> 
<p><img class="alignnone wp-image-2863 size-large" src="https://c1ctech.com/wp-content/uploads/2021/10/R3-e1634662870806-1024x465.png" alt="" width="1024" height="465" /></p> 
<p><img class="alignnone wp-image-2864 size-full" src="https://c1ctech.com/wp-content/uploads/2021/10/R4-e1634662906281.png" alt="" width="758" height="388" /></p> 
<ul> 
<li>We have created three different ads for <span style="color: #008000;"><strong>AdColonyDemo</strong></span> Application.</li> 
</ul> 
<p><img class="alignnone size-large wp-image-2860" src="https://c1ctech.com/wp-content/uploads/2021/10/AllAd-1024x121.png" alt="" width="1024" height="121" /></p> 
<h6> </h6> 
<h4 role="presentation" data-text="Step 1: Set up AdColony"><strong><span class="devsite-heading" style="color: #000080;" role="heading" aria-level="2">Step 3:</span></strong><span style="color: #000080;"><strong> Integrate AdColony ads in app</strong></span></h4> 
<ul> 
<li>At app-level<span style="color: #008000;"><strong> build.gradle</strong></span> file add below dependencies and <span style="color: #0000ff;"><strong>sync</strong></span> project.</li> 
</ul> 
<p><span style="color: #0000ff;"><strong>build.gradle</strong></span></p> 
<pre>dependencies {<br /> implementation 'com.adcolony:sdk:4.6.4'<br /> implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'<br />}</pre> 
<ul> 
<li>In the manifest file add <span style="color: #008000;"><strong>internet</strong></span> and <span style="color: #008000;"><strong>vibrate</strong></span> permission and inside the application, tag add attribute <strong><span style="color: #0000ff;">android:usesCleartextTraffic=”true”</span></strong>. Also, add two AdColony activities inside the application tag, it is requirement for ads.</li> 
</ul> 
<p><span style="color: #0000ff;"><strong>AndroidManifest.xml</strong></span></p> 
<pre><;?xml version="1.0" encoding="utf-8"?>;<br /><;manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.c1ctech.adcolonydemo">;<br /><br /> <span style="color: #008000;"><strong> <;uses-permission android:name="android.permission.INTERNET" />;</strong></span><br /><span style="color: #008000;"><strong> <;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />;</strong></span><br /><span style="color: #008000;"><strong> <;uses-permission android:name="android.permission.VIBRATE" />;</strong></span><br /><br /> <;application<br /> android:allowBackup="true"<br /> android:icon="@mipmap/ic_launcher"<br /> android:label="@string/app_name"<br /> android:roundIcon="@mipmap/ic_launcher_round"<br /> android:supportsRtl="true"<br /> android:theme="@style/Theme.AdColonyDemo"<br /> <strong><span style="color: #008000;">android:usesCleartextTraffic="true"</span></strong>>;<br /> <;activity<br /> android:name=".MainActivity"<br /> android:exported="true">;<br /> <;intent-filter>;<br /> <;action android:name="android.intent.action.MAIN" />;<br /><br /> <;category android:name="android.intent.category.LAUNCHER" />;<br /> <;/intent-filter>;<br /> <;/activity>;<br /> <span style="color: #008000;"><strong> <;activity</strong></span><br /><span style="color: #008000;"><strong> android:name="com.adcolony.sdk.AdColonyInterstitialActivity"</strong></span><br /><span style="color: #008000;"><strong> android:configChanges="keyboardHidden|orientation|screenSize"</strong></span><br /><span style="color: #008000;"><strong> android:hardwareAccelerated="true"</strong></span><br /><span style="color: #008000;"><strong> android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />;</strong></span><br /><span style="color: #008000;"><strong> <;activity</strong></span><br /><span style="color: #008000;"><strong> android:name="com.adcolony.sdk.AdColonyAdViewActivity"</strong></span><br /><span style="color: #008000;"><strong> android:configChanges="keyboardHidden|orientation|screenSize"</strong></span><br /><span style="color: #008000;"><strong> android:hardwareAccelerated="true"</strong></span><br /><span style="color: #008000;"><strong> android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />;</strong></span><br /> <;/application>;<br /><br /><;/manifest>;</pre> 
<ul> 
<li>In <span style="color: #008000;"><strong>proguard-rules.pro</strong></span> file add below ProGuard Configuration.</li> 
</ul> 
<p><span style="color: #0000ff;"><strong>proguard-rules.pro</strong></span></p> 
<pre># For communication with AdColony's WebView<br />-keepclassmembers class * {<br /> @android.webkit.JavascriptInterface <;methods>;;<br />}</pre> 
<h5><span style="color: #000080;"><strong>Creating Layout file</strong></span></h5> 
<p>The <strong><span style="color: #008000;">activity_main.xml</span></strong> file contains a LinearLayout to show banner ad and two buttons on click of which we will show Interstitial and Rewarded video ad.</p> 
<p><strong><span style="color: #0000ff;">activity_main.xml</span></strong></p> 
<pre><;?xml version="1.0" encoding="utf-8"?>;<br /><;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> xmlns:app="http://schemas.android.com/apk/res-auto"<br /> xmlns:tools="http://schemas.android.com/tools"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"<br /> tools:context=".MainActivity">;<br /><br /> <;Button<br /> android:id="@+id/btnInterstitialAd"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_margin="10dp"<br /> android:onClick="showInterstitialAd"<br /> android:padding="15dp"<br /> android:text="Show Interstitial Ad"<br /> app:layout_constraintEnd_toEndOf="parent"<br /> app:layout_constraintHorizontal_bias="0.5"<br /> app:layout_constraintStart_toStartOf="parent"<br /> app:layout_constraintTop_toTopOf="parent"<br /> app:layout_constraintVertical_weight="1" />;<br /><br /> <;Button<br /> android:id="@+id/btnRewardVideoAd"<br /> android:layout_width="match_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_margin="10dp"<br /> android:onClick="showRewardVideoAd"<br /> android:padding="15dp"<br /> android:text="Show Reward Video Ad"<br /> app:layout_constraintEnd_toEndOf="@+id/btnInterstitialAd"<br /> app:layout_constraintStart_toStartOf="@+id/btnInterstitialAd"<br /> app:layout_constraintTop_toBottomOf="@+id/btnInterstitialAd"<br /> app:layout_constraintVertical_weight="1" />;<br /><br /> <;LinearLayout<br /> android:id="@+id/colony"<br /> android:layout_width="match_parent"<br /> android:layout_height="80dp"<br /> android:gravity="center"<br /> android:orientation="vertical"<br /> android:padding="8dp"<br /> app:layout_constraintBottom_toBottomOf="parent"<br /> app:layout_constraintEnd_toEndOf="parent"<br /> app:layout_constraintHorizontal_bias="0.5"<br /> app:layout_constraintStart_toStartOf="parent"<br /> app:layout_constraintVertical_weight="1" />;<br /><;/androidx.constraintlayout.widget.ConstraintLayout>;</pre> 
<ul> 
<li>Inside <strong><span style="color: #008000;">MainActivity.java,</span></strong> add your app id and zone id created for each ad(Banner/Interstitial/Rewarded).</li> 
</ul> 
<pre>private final static String APP_ID = "ENTER_YOUR_APP_ID";<br />private final static String BANNER_ZONE_ID = "ENTER_YOUR_BANNER_ZONE_ID";<br />private final static String INTERSTITIAL_ZONE_ID = "ENTER_YOUR_INTERSTITIAL_ZONE_ID";<br />private final static String REWARD_ZONE_ID = "ENTER_YOUR_REWARD_ZONE_ID";<br />public final static String[] AD_UNIT_Zone_Ids = new String[]{BANNER_ZONE_ID, INTERSTITIAL_ZONE_ID, REWARD_ZONE_ID};</pre> 
<ul> 
<li><strong><span style="color: #0000ff;"> initColonySdk():</span></strong> method which will initialize AdColony SDK.</li> 
</ul> 
<pre>private void initColonySdk() {<br /> <span style="color: #008000;"><strong> // Construct optional app options object to be sent with configure</strong></span><br /><span style="color: #008000;"><strong> // setKeepScreenOn: set a flag on our Activity's window to keep the display from going to sleep.</strong></span><br /> AdColonyAppOptions appOptions = new AdColonyAppOptions().setKeepScreenOn(true);<br /><br /><span style="color: #008000;"><strong> // Configure AdColony in your launching Activity's onCreate() method so that cached ads can</strong></span><br /><span style="color: #008000;"><strong> // be available as soon as possible.</strong></span><br /> AdColony.configure(getApplication(), appOptions, APP_ID, AD_UNIT_Zone_Ids);<br />}</pre> 
<h4><span style="color: #000080;"><strong>Implementing Banner Ad</strong></span></h4> 
<ul> 
<li><strong><span style="color: #0000ff;">initBannerAd()</span></strong> : method will request and load banner ad at the bottom of the screen.</li> 
<li><strong><span style="color: #0000ff;">bannerListener</span></strong>: is defined to write the custom action on a particular ad event.</li> 
</ul> 
<pre>private void initBannerAd() {<br /> bannerContainer = (LinearLayout) findViewById(R.id.colony);<br /><br /> AdColonyAdViewListener bannerListener = new AdColonyAdViewListener() {<br /><br /> <span style="color: #008000;"><strong> // Code to be executed when an ad request is filled.</strong></span><br /><span style="color: #008000;"><strong> // get adColonyAdView object from adcolony Ad Server.</strong></span><br /> @Override<br /> public void onRequestFilled(AdColonyAdView adColonyAdView) {<br /><br /><strong><span style="color: #008000;"> //Remove previous ad view if present.</span></strong><br /> if (bannerContainer.getChildCount() >; 0) {<br /> bannerContainer.removeView(bannerAdColony);<br /> }<br /> bannerContainer.addView(adColonyAdView);<br /> bannerAdColony = adColonyAdView;<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when an ad request is not filled</span></strong><br /><strong><span style="color: #008000;"> // or when an ad is not loaded.</span></strong><br /> @Override<br /> public void onRequestNotFilled(AdColonyZone zone) {<br /> super.onRequestNotFilled(zone);<br /> }<br /><br /><strong><span style="color: #008000;"> //Code to be executed when an ad opens</span></strong><br /> @Override<br /> public void onOpened(AdColonyAdView ad) {<br /> super.onOpened(ad);<br /> }<br /><br /> <span style="color: #008000;"><strong>//Code to be executed when user closed an ad</strong></span><br /> @Override<br /> public void onClosed(AdColonyAdView ad) {<br /> super.onClosed(ad);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when the user clicks on an ad.</span></strong><br /> @Override<br /> public void onClicked(AdColonyAdView ad) {<br /> super.onClicked(ad);<br /> }<br /><br /> <strong><span style="color: #008000;">// called after onAdOpened(), when a user click opens another app</span></strong><br /><strong><span style="color: #008000;"> // (such as the Google Play), backgrounding the current app</span></strong><br /> @Override<br /> public void onLeftApplication(AdColonyAdView ad) {<br /> super.onLeftApplication(ad);<br /> }<br /> };<br /><br /><strong><span style="color: #008000;"> // Optional Ad specific options to be sent with request</span></strong><br /> AdColonyAdOptions adOptions = new AdColonyAdOptions();<br /><br /><strong><span style="color: #008000;"> //Request Ad</span></strong><br /> AdColony.requestAdView(BANNER_ZONE_ID, bannerListener, AdColonyAdSize.BANNER, adOptions);<br />}</pre> 
<h4><span style="color: #000080;"><strong>Implementing Interstitial Ad</strong></span></h4> 
<ul> 
<li><span style="color: #0000ff;"><strong>initInterstitialAd()</strong></span>: method will request and load Interstitial Ad.</li> 
<li><strong><span style="color: #0000ff;">interstitialListener</span></strong>: is defined to write the custom action on a particular ad event.</li> 
</ul> 
<pre>private void initInterstitialAd() {<br /><br /> <span style="color: #008000;"><strong>// Set up listener for interstitial ad callbacks.</strong></span><br /><span style="color: #008000;"><strong> // Implement only the needed callbacks.</strong></span><br /> interstitialListener = new AdColonyInterstitialListener() {<br /><br /><span style="color: #008000;"> <strong>// Code to be executed when an ad request is filled.</strong></span><br /><span style="color: #008000;"><strong> // get AdColonyInterstitial object from adcolony Ad Server.</strong></span><br /> @Override<br /> public void onRequestFilled(AdColonyInterstitial adIn) {<br /><span style="color: #008000;"><strong> // Ad passed back in request filled callback, ad can now be shown</strong></span><br /> interstitialAdColony = adIn;<br /> isInterstitialLoaded = true;<br /> }<br /><br /><span style="color: #008000;"><strong> // Code to be executed when an ad request is not filled</strong></span><br /> @Override<br /> public void onRequestNotFilled(AdColonyZone zone) {<br /> super.onRequestNotFilled(zone);<br /> }<br /><br /><span style="color: #008000;"><strong> //Code to be executed when an ad opens</strong></span><br /> @Override<br /> public void onOpened(AdColonyInterstitial ad) {<br /> super.onOpened(ad);<br /> }<br /><br /> <span style="color: #008000;"><strong> //Code to be executed when user closed an ad</strong></span><br /> @Override<br /> public void onClosed(AdColonyInterstitial ad) {<br /> super.onClosed(ad);<br /> Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();<br /><br /><strong><span style="color: #008000;"> //request new Interstitial Ad on close</span></strong><br /> AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when the user clicks on an ad.</span></strong><br /> @Override<br /> public void onClicked(AdColonyInterstitial ad) {<br /> super.onClicked(ad);<br /> }<br /><br /> <strong><span style="color: #008000;">// called after onAdOpened(), when a user click opens another app</span></strong><br /><strong><span style="color: #008000;"> // (such as the Google Play), backgrounding the current app</span></strong><br /> @Override<br /> public void onLeftApplication(AdColonyInterstitial ad) {<br /> super.onLeftApplication(ad);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when an ad expires.</span></strong><br /> @Override<br /> public void onExpiring(AdColonyInterstitial ad) {<br /> super.onExpiring(ad);<br /> }<br /> };<br /><strong><span style="color: #008000;"> // Optional Ad specific options to be sent with request</span></strong><br /> interstitialAdOptions = new AdColonyAdOptions();<br /> AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);<br />}</pre> 
<ul> 
<li><strong><span style="color: #0000ff;">showInterstitialAd()</span></strong>: 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.</li> 
</ul> 
<pre>public void showInterstitialAd(View view) {<br /> if (interstitialAdColony != null &;&; isInterstitialLoaded) {<br /> interstitialAdColony.show();<br /> isInterstitialLoaded = false;<br /> } else {<br /> Toast.makeText(getApplicationContext(), "Interstitial Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();<br /> }<br />}</pre> 
<h4><span style="color: #000080;"><strong>Implementing Rewarded Video Ad</strong></span></h4> 
<ul> 
<li><strong><span style="color: #0000ff;">initRewardedAd()</span></strong>: method will request and load Reward Ad.</li> 
<li><span style="color: #0000ff;"><strong>rewardListener</strong></span>: is defined to write the custom action on a particular ad event.</li> 
<li><strong><span style="color: #0000ff;">AdColony.setRewardListener():</span></strong> is defined to check the result of the reward video if the reward is watched successfully then we will toast <strong>Reward Earned</strong> else Toast <strong>Reward Cancelled</strong>.</li> 
</ul> 
<pre>private void initRewardedAd() {<br /><br /><strong><span style="color: #008000;"> //setRewardListener:set the AdColonyRewardListener for global reward callbacks for the app.</span></strong><br /> AdColony.setRewardListener(new AdColonyRewardListener() {<br /><br /> @Override<br /> public void onReward(AdColonyReward reward) {<br /><br /> if (reward.success()) {<br /> Toast.makeText(getApplicationContext(), "Reward Earned", Toast.LENGTH_SHORT).show();<br /> } else {<br /> Toast.makeText(getApplicationContext(), "Reward Cancelled", Toast.LENGTH_SHORT).show();<br /> }<br /> }<br /> });<br /> <span style="color: #008000;"><strong>// Set up listener for interstitial ad callbacks. You only need to implement the callbacks</strong></span><br /><span style="color: #008000;"><strong> // that you care about.</strong></span><br /> rewardListener = new AdColonyInterstitialListener() {<br /><br /> <strong><span style="color: #008000;">// Code to be executed when an ad request is filled</span></strong><br /><strong><span style="color: #008000;"> // get AdColonyInterstitial Reward object from adcolony Ad Server</span></strong><br /> @Override<br /> public void onRequestFilled(AdColonyInterstitial adReward) {<br /><strong><span style="color: #008000;"> // Ad passed back in request filled callback, ad can now be shown</span></strong><br /> rewardAdColony = adReward;<br /> isRewardLoaded = true;<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when an ad request is not filled</span></strong><br /> @Override<br /> public void onRequestNotFilled(AdColonyZone zone) {<br /> }<br /><br /><strong><span style="color: #008000;"> //Code to be executed when an ad opens</span></strong><br /> @Override<br /> public void onOpened(AdColonyInterstitial ad) {<br /> super.onOpened(ad);<br /> }<br /><br /><span style="color: #008000;"><strong> //Code to be executed when user closed an ad </strong></span><br /> @Override<br /> public void onClosed(AdColonyInterstitial ad) {<br /> super.onClosed(ad);<br /> Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();<br /><br /><strong><span style="color: #008000;"> //request new reward on close</span></strong><br /> AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when the user clicks on an ad.</span></strong><br /> @Override<br /> public void onClicked(AdColonyInterstitial ad) {<br /> super.onClicked(ad);<br /> }<br /><br /> <strong><span style="color: #008000;">// called after onAdOpened(), when a user click opens another app</span></strong><br /><strong><span style="color: #008000;"> // (such as the Google Play), backgrounding the current app</span></strong><br /> @Override<br /> public void onLeftApplication(AdColonyInterstitial ad) {<br /> super.onLeftApplication(ad);<br /> }<br /><br /><span style="color: #008000;"><strong> // Code to be executed when an ad expires.</strong></span><br /> @Override<br /> public void onExpiring(AdColonyInterstitial ad) {<br /> super.onExpiring(ad);<br /> }<br /> };<br /><strong><span style="color: #008000;"> // Ad specific options to be sent with request</span></strong><br /> rewardAdOptions = new AdColonyAdOptions()<br /> .enableConfirmationDialog(false)<br /> .enableResultsDialog(false);<br /> AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);<br />}</pre> 
<ul> 
<li><span style="color: #0000ff;"><strong>showRewardVideoAd(): </strong></span>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.</li> 
</ul> 
<pre>public void showRewardVideoAd(View view) {<br /> if (rewardAdColony != null &;&; isRewardLoaded) {<br /> rewardAdColony.show();<br /> isRewardLoaded = false;<br /> } else {<br /> Toast.makeText(getApplicationContext(), "Reward Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();<br /> }<br />}</pre> 
<h4><span style="color: #000080;"><strong>Complete Code</strong></span></h4> 
<p>The <span style="color: #008000;"><strong>MainActivity.java</strong></span> file contains the following code:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>
 
<p><strong><span style="color: #0000ff;">MainActivity.java </span></strong></p> 
<pre>package com.c1ctech.adcolonydemo;<br /><br />import androidx.appcompat.app.AppCompatActivity;<br />import com.adcolony.sdk.*;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.LinearLayout;<br />import android.widget.Toast;<br /><br />public class MainActivity extends AppCompatActivity {<br /><br /> private final static String APP_ID = "ENTER_YOUR_APP_ID";<br /> private final static String BANNER_ZONE_ID = "ENTER_YOUR_BANNER_ZONE_ID";<br /> private final static String INTERSTITIAL_ZONE_ID = "ENTER_YOUR_INTERSTITIAL_ZONE_ID";<br /> private final static String REWARD_ZONE_ID = "ENTER_YOUR_REWARD_ZONE_ID";<br /> public final static String[] AD_UNIT_Zone_Ids = new String[]{BANNER_ZONE_ID, INTERSTITIAL_ZONE_ID, REWARD_ZONE_ID};<br /><br /> private LinearLayout bannerContainer;<br /> private AdColonyAdView bannerAdColony;<br /><br /> private AdColonyInterstitial interstitialAdColony;<br /> private AdColonyInterstitialListener interstitialListener;<br /> private AdColonyAdOptions interstitialAdOptions;<br /><br /> private AdColonyInterstitial rewardAdColony;<br /> private AdColonyInterstitialListener rewardListener;<br /> private AdColonyAdOptions rewardAdOptions;<br /><br /> private static boolean isInterstitialLoaded;<br /> private static boolean isRewardLoaded;<br /><br /><br /> @Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.activity_main);<br /><br /> initColonySdk();<br /> initBannerAd();<br /> initInterstitialAd();<br /> initRewardedAd();<br /> }<br /><br /> private void initColonySdk() {<br /> <span style="color: #008000;"><strong>// Construct optional app options object to be sent with configure</strong></span><br /><span style="color: #008000;"><strong> // setKeepScreenOn: set a flag on our Activity's window to keep the display from going to sleep.</strong></span><br /> AdColonyAppOptions appOptions = new AdColonyAppOptions().setKeepScreenOn(true);<br /><br /> <strong><span style="color: #008000;"> // Configure AdColony in your launching Activity's onCreate() method so that cached ads can</span></strong><br /><strong><span style="color: #008000;"> // be available as soon as possible.</span></strong><br /> AdColony.configure(getApplication(), appOptions, APP_ID, AD_UNIT_Zone_Ids);<br /> }<br /><br /> private void initBannerAd() {<br /> bannerContainer = (LinearLayout) findViewById(R.id.colony);<br /><br /> AdColonyAdViewListener bannerListener = new AdColonyAdViewListener() {<br /><br /> <strong><span style="color: #008000;"> // Code to be executed when an ad request is filled</span></strong><br /><strong><span style="color: #008000;"> // or when an ad finishes loading.</span></strong><br /> @Override<br /> public void onRequestFilled(AdColonyAdView adColonyAdView) {<br /><br /><strong><span style="color: #008000;"> //Remove previous ad view if present.</span></strong><br /> if (bannerContainer.getChildCount() >; 0) {<br /> bannerContainer.removeView(bannerAdColony);<br /> }<br /> bannerContainer.addView(adColonyAdView);<br /> bannerAdColony = adColonyAdView;<br /> }<br /><br /> <strong><span style="color: #008000;">// Code to be executed when an ad request is not filled</span></strong><br /><strong><span style="color: #008000;"> //or when an ad is not loaded.</span></strong><br /> @Override<br /> public void onRequestNotFilled(AdColonyZone zone) {<br /> super.onRequestNotFilled(zone);<br /> }<br /><br /><strong><span style="color: #008000;"> //Code to be executed when an ad opens</span></strong><br /> @Override<br /> public void onOpened(AdColonyAdView ad) {<br /> super.onOpened(ad);<br /> }<br /><br /> <span style="color: #008000;"><strong>//Code to be executed when user closed an ad</strong></span><br /> @Override<br /> public void onClosed(AdColonyAdView ad) {<br /> super.onClosed(ad);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when the user clicks on an ad.</span></strong><br /> @Override<br /> public void onClicked(AdColonyAdView ad) {<br /> super.onClicked(ad);<br /> }<br /><br /> <strong><span style="color: #008000;"> // called after onAdOpened(), when a user click opens another app</span></strong><br /><strong><span style="color: #008000;"> // (such as the Google Play), backgrounding the current app</span></strong><br /> @Override<br /> public void onLeftApplication(AdColonyAdView ad) {<br /> super.onLeftApplication(ad);<br /> }<br /> };<br /><br /><strong><span style="color: #008000;"> // Optional Ad specific options to be sent with request</span></strong><br /> AdColonyAdOptions adOptions = new AdColonyAdOptions();<br /><br /><strong><span style="color: #008000;"> //Request Ad</span></strong><br /> AdColony.requestAdView(BANNER_ZONE_ID, bannerListener, AdColonyAdSize.BANNER, adOptions);<br /> }<br /><br /> private void initInterstitialAd() {<br /><br /> <strong><span style="color: #008000;">// Set up listener for interstitial ad callbacks.</span></strong><br /><strong><span style="color: #008000;"> // Implement only the needed callbacks.</span></strong><br /> interstitialListener = new AdColonyInterstitialListener() {<br /><br /> <strong><span style="color: #008000;"> // Code to be executed when an ad request is filled.</span></strong><br /><strong><span style="color: #008000;"> // get AdColonyInterstitial object from adcolony Ad Server.</span></strong><br /> @Override<br /> public void onRequestFilled(AdColonyInterstitial adIn) {<br /><span style="color: #008000;"><strong> // Ad passed back in request filled callback, ad can now be shown</strong></span><br /> interstitialAdColony = adIn;<br /> isInterstitialLoaded = true;<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when an ad request is not filled</span></strong><br /> @Override<br /> public void onRequestNotFilled(AdColonyZone zone) {<br /> super.onRequestNotFilled(zone);<br /> }<br /><br /><span style="color: #008000;"><strong> //Code to be executed when an ad opens</strong></span><br /> @Override<br /> public void onOpened(AdColonyInterstitial ad) {<br /> super.onOpened(ad);<br /> }<br /><br /> <strong><span style="color: #008000;">//Code to be executed when user closed an ad</span></strong><br /> @Override<br /> public void onClosed(AdColonyInterstitial ad) {<br /> super.onClosed(ad);<br /> Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();<br /><br /><strong><span style="color: #008000;"> //request new Interstitial Ad on close</span></strong><br /> AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when the user clicks on an ad.</span></strong><br /> @Override<br /> public void onClicked(AdColonyInterstitial ad) {<br /> super.onClicked(ad);<br /> }<br /><br /> <strong><span style="color: #008000;">// called after onAdOpened(), when a user click opens another app</span></strong><br /><strong><span style="color: #008000;"> // (such as the Google Play), backgrounding the current app</span></strong><br /> @Override<br /> public void onLeftApplication(AdColonyInterstitial ad) {<br /> super.onLeftApplication(ad);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when an ad expires.</span></strong><br /> @Override<br /> public void onExpiring(AdColonyInterstitial ad) {<br /> super.onExpiring(ad);<br /> }<br /> };<br /><strong><span style="color: #008000;"> // Optional Ad specific options to be sent with request</span></strong><br /> interstitialAdOptions = new AdColonyAdOptions();<br /> AdColony.requestInterstitial(INTERSTITIAL_ZONE_ID, interstitialListener, interstitialAdOptions);<br /> }<br /><br /> private void initRewardedAd() {<br /><br /><span style="color: #008000;"><strong> //setRewardListener:set the AdColonyRewardListener for global reward callbacks for the app.</strong></span><br /> AdColony.setRewardListener(new AdColonyRewardListener() {<br /><br /> @Override<br /> public void onReward(AdColonyReward reward) {<br /><br /> if (reward.success()) {<br /> Toast.makeText(getApplicationContext(), "Reward Earned", Toast.LENGTH_SHORT).show();<br /> } else {<br /> Toast.makeText(getApplicationContext(), "Reward Cancelled", Toast.LENGTH_SHORT).show();<br /> }<br /> }<br /> });<br /> <strong><span style="color: #008000;">// Set up listener for interstitial ad callbacks. </span></strong><br /><strong><span style="color: #008000;"> // Implement only the needed callbacks.</span></strong><br /> rewardListener = new AdColonyInterstitialListener() {<br /><br /> <strong><span style="color: #008000;"> // Code to be executed when an ad request is filled</span></strong><br /><strong><span style="color: #008000;"> // get AdColonyInterstitial Reward object from adcolony Ad Server</span></strong><br /> @Override<br /> public void onRequestFilled(AdColonyInterstitial adReward) {<br /><strong><span style="color: #008000;"> // Ad passed back in request filled callback, ad can now be shown</span></strong><br /> rewardAdColony = adReward;<br /> isRewardLoaded = true;<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when an ad request is not filled</span></strong><br /> @Override<br /> public void onRequestNotFilled(AdColonyZone zone) {<br /> }<br /><br /><strong><span style="color: #008000;"> //Code to be executed when an ad opens</span></strong><br /> @Override<br /> public void onOpened(AdColonyInterstitial ad) {<br /> super.onOpened(ad);<br /> }<br /><br /> <strong><span style="color: #008000;"> //Code to be executed when user closed an ad</span></strong><br /> @Override<br /> public void onClosed(AdColonyInterstitial ad) {<br /> super.onClosed(ad);<br /> Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();<br /><br /><strong><span style="color: #008000;"> //request new reward on close</span></strong><br /> AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when the user clicks on an ad.</span></strong><br /> @Override<br /> public void onClicked(AdColonyInterstitial ad) {<br /> super.onClicked(ad);<br /> }<br /><br /> <strong><span style="color: #008000;"> // called after onAdOpened(), when a user click opens another app</span></strong><br /><strong><span style="color: #008000;"> // (such as the Google Play), backgrounding the current app</span></strong><br /> @Override<br /> public void onLeftApplication(AdColonyInterstitial ad) {<br /> super.onLeftApplication(ad);<br /> }<br /><br /><strong><span style="color: #008000;"> // Code to be executed when an ad expires.</span></strong><br /> @Override<br /> public void onExpiring(AdColonyInterstitial ad) {<br /> super.onExpiring(ad);<br /> }<br /> };<br /><strong><span style="color: #008000;"> // Ad specific options to be sent with request</span></strong><br /> rewardAdOptions = new AdColonyAdOptions()<br /> .enableConfirmationDialog(false)<br /> .enableResultsDialog(false);<br /> AdColony.requestInterstitial(REWARD_ZONE_ID, rewardListener, rewardAdOptions);<br /> }<br /><br /> public void showInterstitialAd(View view) {<br /> if (interstitialAdColony != null &;&; isInterstitialLoaded) {<br /> interstitialAdColony.show();<br /> isInterstitialLoaded = false;<br /> } else {<br /> Toast.makeText(getApplicationContext(), "Interstitial Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();<br /> }<br /> }<br /><br /> public void showRewardVideoAd(View view) {<br /> if (rewardAdColony != null &;&; isRewardLoaded) {<br /> rewardAdColony.show();<br /> isRewardLoaded = false;<br /> } else {<br /> Toast.makeText(getApplicationContext(), "Reward Ad Is Not Loaded Yet or Request Not Filled", Toast.LENGTH_SHORT).show();<br /> }<br /> }<br />}</pre> 
<h4><span style="color: #000080;">Step 4: Run and test your app</span></h4> 
<p>When you run your app it will look like this:</p> 
<p><img class="alignnone wp-image-2875" src="https://c1ctech.com/wp-content/uploads/2021/10/Screenshot_20211020-132419_AdColonyDemo-498x1024.jpg" alt="" width="289" height="595" /> <span style="font-size: 16px;"> </span><img class="alignnone wp-image-2877" style="font-style: inherit; font-weight: inherit;" src="https://c1ctech.com/wp-content/uploads/2021/10/Screenshot_20211020-132459_AdColonyDemo-498x1024.jpg" alt="" width="291" height="599" /></p> 
<p> ;</p> 


