Getting started with AdTech Mobile Android SDK

AdTech is a powerful and easy-to-use ad mediation platform. Our goal is to serve your applications the best ads that pay the most at all time.

Prerequisites

Import the AdTech Mobile SDK

Using maven repository

Project-level build.gradle
Add AdTech’s maven repository:

allprojects {
    repositories {
        google()
        jcenter()
        // ...
        maven {
            url "http://ad-tech-releases.s3.amazonaws.com"
        }
    }
}

App-level build.gradle
Add AdTech, Google & Facebook dependecies:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    // ...
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72' // Java only
    implementation 'com.google.android.gms:play-services-ads:19.0.1'
    implementation 'com.facebook.android:audience-network-sdk:5.+'
    implementation 'com.adtech.adtechmobile:adtechmobile:1.+@aar'
}

Update your AndroidManifest.xml

Add your AdMob’s application ID (which is provided for you by AdTech) to your app’s AndroidManifest.xml file by adding a <meta-data> tag with name com.google.android.gms.ads.APPLICATION_ID, as shown below.

<manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>

Initialize Mobile Ads SDK

Before loading ads, your app needs to initialize the AdTechSDK by calling ADTechMobile.initialize(). This needs to be done only once, ideally at app launch.

// Syntax: Kotlin
package ...
import ...
import com.adtech.adtechmobile.*

class MainActivity : AppCompatActivity() {
...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Initialize
        ADTechMobile.initialize(this)
    }
...
}
// Syntax: Java
package ...;

import ...
import com.adtech.adtechmobile.*;

public class MainActivity extends AppCompatActivity {
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ADTechMobile.initialize(this);
    }
...
}

Select an Ad format

Banner ads occupy a spot within an app’s layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time.

Create an ADTBannerView

The first step toward displaying a banner is to place ADTBannerView in the layout for the Activity or Fragment in which you’d like to display it.

Add an ADTBannerView to a layout

The easiest way to do this is to add one to the corresponding XML layout file. Here’s an example that shows an activity’s ADTBannerView:

# main_activity.xml
...
<com.adtech.adtechmobile.ADTBannerView
    android:id="@+id/bannerAd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">
</com.adtech.adtechmobile.ADTBannerView>
...
Add an ADTBannerView programmatically

You can alternatively create ADTBannerView programmatically:

// Syntax: Kotlin
// Create banner ad
val adView = ADTBannerView(this)
// Tell parent layout how to render the adview
val layout = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
adView.layoutParams = layout
// Add to parent layout
activityLayout.addView(adView)
// Syntax: Java
// Create banner ad
ADTBannerView adView = new ADTBannerView(this);
// Tell parent layout how to render the adview
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
adView.setLayoutParams(layoutParams);
ViewGroup layout = (ViewGroup) findViewById(R.id.activityLayout);
// Add to parent layout
layout.addView(adView);

Choose AdSize

We currently support HEIGHT_50, HEIGHT_90 and HEIGHT_250

// Syntax: Kotlin
adView.adSize = ADTAdSize.HEIGHT_50
// Syntax: Java
adView.setAdSize(ADTAdSize.HEIGHT_50);

Set testMode

When building and testing your apps, make sure you use test ads rather than live, production ads:

// Syntax: Kotlin
ADTRequestConfiguration.testMode = true
// Syntax: Java
ADTRequestConfiguration.testMode = true;

Request an Ad

Once the AdBannerView is in place, the next step is to load an ad. That’s done with the request() method in the AdBannerView class:

// Syntax: Kotlin
// Send ad request
adView.request()
// Syntax: Java
// Send ad request
adView.request();

That’s it! Your app is now ready to display banner ads.

Override AdEvents

To further customize the behavior of your ad, you can hook onto a number of events in the ad’s lifecycle: loading, opening, closing, and so on. You can listen for these events through the ADTAdListener class:

// Syntax: Kotlin
adView.adListener = object : ADTAdListener {
    override fun onAdLoaded() {
        Log.d("Test App", "ADT Banner loaded")
    }

    override fun onAdFailedToLoad(errorCode: ADTAdError) {
        Log.d("Test App", "ADT Banner failed: code " + errorCode)
    }

    override fun onAdOpened() {
    }

    override fun onAdClosed() {
    }

    override fun onAdLeftApplication() {
    }

    override fun onAdClicked() {
    }
}
// Syntax: Java
adView.setAdListener(new ADTAdListener() {
    @Override
    public void onAdLoaded() {
        Log.d("Test App", "ADT Banner loaded");
    }

    @Override
    public void onAdLoaded(@NotNull ADTNativeAd adtNativeAd) { }

    @Override
    public void onAdFailedToLoad(@NotNull ADTAdError adtAdError) {
        Log.d("Test App", "ADT Banner failed: code " + adtAdError);
    }

    @Override
    public void onAdClicked() {
        Log.d("Test App", "ADT Banner loaded");
    }

    @Override
    public void onAdOpened() {
    }

    @Override
    public void onAdLeftApplication() {
    }

    @Override
    public void onAdClosed() {
    }
});

Interstitial

Interstitial ads are full-screen ads that cover the interface of their host app. They’re typically displayed at natural transition points in the flow of an app, such as between activities or during the pause between levels in a game. When an app shows an interstitial ad, the user has the choice to either tap on the ad and continue to its destination or close it and return to the app.

Create an Interstitial Ad object

Interstitial ads are requested and shown by ADTInterstitial objects. The first step is instantiating ADTInterstitial. This is done in the onCreate() method of an Activity:

// Syntax: kotlin
package ...

import com.adtech.adtechmobile.*

class MainActivity : AppCompatActivity() {

    private lateinit var adtInterAd: ADTInterstitial

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize
        ADTechMobile.initialize(this)
        adtInterAd = ADTInterstitial(this)
    }
}
// Syntax: Java
package ...

import com.adtech.adtechmobile.*;

public class MainActivity extends AppCompatActivity {

    private ADTInterstitial adtInterAd;

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

        ADTechMobile.initialize(this);

        adtInterAd = new ADTInterstitial(this);
    }
}

Override AdEvents

To further customize the behavior of your ad, you can hook onto a number of events in the ad’s lifecycle: loading, opening, closing, and so on. You can listen for these events through the ADTAdListener class.

// Syntax: Kotlin
adtInterAd.adListener = object : ADTAdListener {
    override fun onAdLoaded() {
        Log.d("Test App", "ADT Interstitial loaded")
    }

    override fun onAdFailedToLoad(errorCode: ADTAdError) {
        Log.d("Test App", "ADT Interstitial failed: code " + errorCode)
    }

    override fun onAdOpened() {
    }

    override fun onAdClosed() {
    }

    override fun onAdLeftApplication() {
    }

    override fun onAdClicked() {
    }
}
// Syntax: Java
adtInterAd.setAdListener(new ADTAdListener() {
    @Override
    public void onAdLoaded(@NotNull ADTNativeAd adtNativeAd) { }

    @Override
    public void onAdLoaded() {
        Log.d("Test App", "ADT Interstitial loaded");
    }

    @Override
    public void onAdFailedToLoad(@NotNull ADTAdError adtAdError) {
        Log.d("Test App", "ADT Interstitial failed: code " + adtAdError);
    }

    @Override
    public void onAdClicked() { }

    @Override
    public void onAdOpened() { }

    @Override
    public void onAdLeftApplication() { }

    @Override
    public void onAdClosed() { }
});

Set testMode

When building and testing your apps, make sure you use test ads rather than live, production ads:

// Syntax: Kotlin
// Test mode, remove this to get data from ADT Server
ADTRequestConfiguration.testMode = true
// Syntax: Java
// Test mode, remove this to get data from ADT Server
ADTRequestConfiguration.testMode = true;

Request an interstitial Ad

To load an interstitial ad, call the ADTInterstitial object’s request() method:

// Syntax: Kotlin
adtInterAd.request()
// Syntax: Java
adtInterAd.request();

Show the Ad

Interstitial ads should be displayed during natural pauses in the flow of an app. Between levels of a game is a good example, or after the user completes a task. To show an interstitial, use the isReady attribute to verify that it’s done loading, then call show(). The interstitial ad from the previous code example could be shown in a button’s OnClickListener like this:

// Syntax: Kotlin
myButton.setOnClickListener {
    if (adtInterAd.isReady) {
        adtInterAd.show()
    }
}

// Syntax: Java
Button myButton = findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (adtInterAd.isReady()) {
            adtInterAd.show();
        }
    }
});

Using AdListener to reload

The ADTAdListener class’s onAdClosed() method is a handy place to load a new interstitial after displaying the previous one

// Syntax: Kotlin
adtInterAd.adListener = object : ADTAdListener {
    override fun onAdLoaded() {
        Log.d("Test App", "ADT Interstitial loaded")
    }

    override fun onAdFailedToLoad(errorCode: ADTAdError) {
        Log.d("Test App", "ADT Interstitial failed: code " + errorCode)
    }

    override fun onAdClosed() {
        adtInterAd.request()
    }

    override fun onAdOpened() {
    }

    override fun onAdLeftApplication() {
    }

    override fun onAdClicked() {
    }
}
// Syntax: Java
adtInterAd.setAdListener(new ADTAdListener() {
    @Override
    public void onAdLoaded(@NotNull ADTNativeAd adtNativeAd) { }

    @Override
    public void onAdLoaded() {
        Log.d("Test App", "ADT Interstitial loaded")
    }

    @Override
    public void onAdFailedToLoad(@NotNull ADTAdError adtAdError) {
        Log.d("Test App", "ADT Interstitial failed: code " + adtAdError);
    }

    @Override
    public void onAdClosed() {
        adtInterAd.request();
    }

    @Override
    public void onAdClicked() { }

    @Override
    public void onAdOpened() { }

    @Override
    public void onAdLeftApplication() { }
});

Demo applications

You can access Android demo apps:


top