Skip to main content

Overview

Check out the example code and sample projects for the Adrop Android SDK.

Sample Project

You can find the complete sample project on GitHub.

Android Sample Project

Sample Android app written in Kotlin

Quick Start Examples

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.adrop.ads.banner.AdropBanner

class BannerActivity : AppCompatActivity() {
    private lateinit var adropBanner: AdropBanner

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

        // Initialize banner ad
        adropBanner = findViewById(R.id.adrop_banner)
        adropBanner.unitId = "YOUR_UNIT_ID"

        // Set ad listener (optional)
        adropBanner.listener = object : AdropBanner.Listener {
            override fun onAdReceived(banner: AdropBanner) {
                // Ad received successfully
            }

            override fun onAdClicked(banner: AdropBanner) {
                // Ad clicked
            }

            override fun onAdFailedToReceive(banner: AdropBanner, errorCode: Int) {
                // Failed to receive ad
            }
        }

        // Load ad
        adropBanner.load()
    }
}
Layout file (activity_banner.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <io.adrop.ads.banner.AdropBanner
        android:id="@+id/adrop_banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Native Ad (Kotlin)

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import io.adrop.ads.native.AdropNativeAd
import io.adrop.ads.native.AdropNativeAdView

class NativeAdActivity : AppCompatActivity() {
    private lateinit var nativeAd: AdropNativeAd
    private lateinit var nativeAdView: AdropNativeAdView

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

        // Initialize native ad
        nativeAd = AdropNativeAd(this, "YOUR_UNIT_ID")

        // Set ad listener
        nativeAd.listener = object : AdropNativeAd.Listener {
            override fun onAdReceived(ad: AdropNativeAd) {
                // Ad received successfully - bind UI
                displayNativeAd(ad)
            }

            override fun onAdClicked(ad: AdropNativeAd) {
                // Ad clicked
            }

            override fun onAdFailedToReceive(ad: AdropNativeAd, errorCode: Int) {
                // Failed to receive ad
            }
        }

        // Load ad
        nativeAd.load()
    }

    private fun displayNativeAd(ad: AdropNativeAd) {
        // Inflate native ad view
        val inflater = LayoutInflater.from(this)
        nativeAdView = inflater.inflate(
            R.layout.native_ad_layout,
            null
        ) as AdropNativeAdView

        // Bind ad elements
        val iconView = nativeAdView.findViewById<ImageView>(R.id.ad_icon)
        val titleView = nativeAdView.findViewById<TextView>(R.id.ad_title)
        val bodyView = nativeAdView.findViewById<TextView>(R.id.ad_body)
        val ctaButton = nativeAdView.findViewById<TextView>(R.id.ad_cta)

        // Set ad data
        ad.icon?.let { iconView.setImageBitmap(it) }
        titleView.text = ad.title
        bodyView.text = ad.body
        ctaButton.text = ad.callToAction

        // Register ad with native ad view
        nativeAdView.setNativeAd(ad)

        // Add to container
        val adContainer = findViewById<View>(R.id.native_ad_container)
        adContainer.removeAllViews()
        adContainer.addView(nativeAdView)
    }

    override fun onDestroy() {
        nativeAd.destroy()
        super.onDestroy()
    }
}
Native ad layout (native_ad_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<io.adrop.ads.native.AdropNativeAdView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/ad_icon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/ad_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/ad_icon"
            android:layout_marginStart="12dp"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/ad_body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/ad_title"
            android:layout_toEndOf="@id/ad_icon"
            android:layout_marginStart="12dp"
            android:layout_marginTop="4dp"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/ad_cta"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ad_body"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="12dp"
            android:background="@drawable/button_background"
            android:padding="12dp"
            android:textColor="@android:color/white"
            android:textSize="14sp"
            android:textStyle="bold" />

    </RelativeLayout>

</io.adrop.ads.native.AdropNativeAdView>

Interstitial Ad (Kotlin)

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import io.adrop.ads.interstitial.AdropInterstitialAd

class InterstitialActivity : AppCompatActivity() {
    private var interstitialAd: AdropInterstitialAd? = null
    private lateinit var showButton: Button

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

        showButton = findViewById(R.id.show_interstitial_button)
        showButton.isEnabled = false

        // Load interstitial ad
        loadInterstitialAd()

        showButton.setOnClickListener {
            showInterstitialAd()
        }
    }

    private fun loadInterstitialAd() {
        interstitialAd = AdropInterstitialAd(this, "YOUR_UNIT_ID")

        interstitialAd?.listener = object : AdropInterstitialAd.Listener {
            override fun onAdReceived(ad: AdropInterstitialAd) {
                // Ad loaded - ready to show
                showButton.isEnabled = true
            }

            override fun onAdFailedToReceive(ad: AdropInterstitialAd, errorCode: Int) {
                // Failed to load ad
                showButton.isEnabled = false
            }

            override fun onAdFailedToShow(ad: AdropInterstitialAd, errorCode: Int) {
                // Failed to show ad
            }

            override fun onAdDidPresentFullScreen(ad: AdropInterstitialAd) {
                // Interstitial ad displayed on screen
            }

            override fun onAdDidDismissFullScreen(ad: AdropInterstitialAd) {
                // Interstitial ad closed - load new ad
                loadInterstitialAd()
            }

            override fun onAdClicked(ad: AdropInterstitialAd) {
                // Ad clicked
            }
        }

        interstitialAd?.load()
    }

    private fun showInterstitialAd() {
        interstitialAd?.let { ad ->
            if (ad.isLoaded) {
                ad.show()
            }
        }
    }

    override fun onDestroy() {
        interstitialAd?.destroy()
        super.onDestroy()
    }
}

Rewarded Ad (Kotlin)

import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import io.adrop.ads.rewarded.AdropRewardedAd

class RewardedAdActivity : AppCompatActivity() {
    private var rewardedAd: AdropRewardedAd? = null
    private lateinit var showButton: Button

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

        showButton = findViewById(R.id.show_rewarded_button)
        showButton.isEnabled = false

        // Load rewarded ad
        loadRewardedAd()

        showButton.setOnClickListener {
            showRewardedAd()
        }
    }

    private fun loadRewardedAd() {
        rewardedAd = AdropRewardedAd(this, "YOUR_UNIT_ID")

        rewardedAd?.listener = object : AdropRewardedAd.Listener {
            override fun onAdReceived(ad: AdropRewardedAd) {
                // Ad loaded - ready to show
                showButton.isEnabled = true
            }

            override fun onAdFailedToReceive(ad: AdropRewardedAd, errorCode: Int) {
                // Failed to load ad
                showButton.isEnabled = false
            }

            override fun onAdFailedToShow(ad: AdropRewardedAd, errorCode: Int) {
                // Failed to show ad
            }

            override fun onAdDidPresentFullScreen(ad: AdropRewardedAd) {
                // Full screen ad displayed
            }

            override fun onAdDidDismissFullScreen(ad: AdropRewardedAd) {
                // Ad closed - load new ad
                loadRewardedAd()
            }

            override fun onAdClicked(ad: AdropRewardedAd) {
                // Ad clicked
            }

            override fun onAdEarnRewardHandler(ad: AdropRewardedAd, type: Int, amount: Int) {
                // Grant reward
                Toast.makeText(
                    this@RewardedAdActivity,
                    "Reward earned: $amount of type $type",
                    Toast.LENGTH_SHORT
                ).show()

                // Implement actual reward granting logic here
                grantReward(type, amount)
            }
        }

        rewardedAd?.load()
    }

    private fun showRewardedAd() {
        rewardedAd?.let { ad ->
            if (ad.isLoaded) {
                ad.show()
            }
        }
    }

    private fun grantReward(type: Int, amount: Int) {
        // Logic to grant reward to user
        // e.g., game coins, lives, items, etc.
    }

    override fun onDestroy() {
        rewardedAd?.destroy()
        super.onDestroy()
    }
}

Java Examples

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import io.adrop.ads.banner.AdropBanner;

public class BannerActivity extends AppCompatActivity {
    private AdropBanner adropBanner;

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

        // Initialize banner ad
        adropBanner = findViewById(R.id.adrop_banner);
        adropBanner.setUnitId("YOUR_UNIT_ID");

        // Set ad listener
        adropBanner.setListener(new AdropBanner.Listener() {
            @Override
            public void onAdReceived(AdropBanner banner) {
                // Ad received successfully
            }

            @Override
            public void onAdClicked(AdropBanner banner) {
                // Ad clicked
            }

            @Override
            public void onAdFailedToReceive(AdropBanner banner, int errorCode) {
                // Failed to receive ad
            }
        });

        // Load ad
        adropBanner.load();
    }
}

Interstitial Ad (Java)

import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import io.adrop.ads.interstitial.AdropInterstitialAd;

public class InterstitialActivity extends AppCompatActivity {
    private AdropInterstitialAd interstitialAd;
    private Button showButton;

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

        showButton = findViewById(R.id.show_interstitial_button);
        showButton.setEnabled(false);

        loadInterstitialAd();

        showButton.setOnClickListener(v -> showInterstitialAd());
    }

    private void loadInterstitialAd() {
        interstitialAd = new AdropInterstitialAd(this, "YOUR_UNIT_ID");

        interstitialAd.setListener(new AdropInterstitialAd.Listener() {
            @Override
            public void onAdReceived(AdropInterstitialAd ad) {
                showButton.setEnabled(true);
            }

            @Override
            public void onAdFailedToReceive(AdropInterstitialAd ad, int errorCode) {
                showButton.setEnabled(false);
            }

            @Override
            public void onAdFailedToShow(AdropInterstitialAd ad, int errorCode) {
                // Failed to show ad
            }

            @Override
            public void onAdDidPresentFullScreen(AdropInterstitialAd ad) {
                // Ad displayed
            }

            @Override
            public void onAdDidDismissFullScreen(AdropInterstitialAd ad) {
                // Ad closed - load new ad
                loadInterstitialAd();
            }

            @Override
            public void onAdClicked(AdropInterstitialAd ad) {
                // Ad clicked
            }
        });

        interstitialAd.load();
    }

    private void showInterstitialAd() {
        if (interstitialAd != null && interstitialAd.isLoaded()) {
            interstitialAd.show();
        }
    }

    @Override
    protected void onDestroy() {
        if (interstitialAd != null) {
            interstitialAd.destroy();
        }
        super.onDestroy();
    }
}

Advanced Examples

Dark Mode Support

import io.adrop.ads.AdropSDK

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Set dark mode
        val isDarkMode = resources.configuration.uiMode and
            Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

        AdropSDK.setDarkMode(isDarkMode)
    }
}

Test Mode

import io.adrop.ads.AdropSDK

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        // Initialize
        AdropSDK.initialize(this, "YOUR_PRODUCTION_KEY")

        // Enable test mode only during development
        if (BuildConfig.DEBUG) {
            AdropSDK.setTestMode(true)
        }
    }
}