Android Standalone Ad

Learn how to create additional content monetization opportunities in your Android app

Standalone Ads enable you to customize ad opportunities and augment the revenue potential of your app:

  • Create new high-profile ad opportunities of any dimensions
  • Display video and display ads
  • Enforce brand safety


Prerequisites

  • Android Studio
  • Minimum OS Version: API Level 21
  • Gradle Version: 8.1
  • Android Gradle Plugin: 4.1.3
  • Google Mobile Ads App ID


Create an Application class

Use the following steps to create a class inheriting from the Application class for the later initialization of the SDK:

  1. In the src/main/java directory, create a new file called App.kt.

    πŸ“˜

    This file can be given any name. If you choose a different name, be sure to make the appropriate adjustments in the steps throughout this article.

  2. In App.kt, define the App class by extending the Application class. The Application class is the base class for maintaining the global application state.

    class App : Application() {}
    
  3. In the App class, add any needed setup code to onCreate().

    class App : Application() {
        override fun onCreate() {
            super.onCreate()
    				
            // SDK Initialization will be here
        }
    }
    
  4. In AndroidManifest.xml, add the android:name=".App" attribute to direct Android to use your custom App class as the main Application class.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
        <application
            android:name=".App"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="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>
        </application>
    </manifest>
    


Add the internet permission

To allow the app to use the internet connection of the device, the INTERNET permission must be enabled.

Use the following steps to enable this permission:

  1. In AndroidManifest.xml, add the INTERNET permission inside the <manifest> element.
    1. <uses-permission android:name="android.permission.INTERNET" />
    
  2. Sync the project.


Add the dependencies

Use the following steps to add the SpotIM SDK to your project

  1. In the main build.gradle or settings.gradle, add the following Maven repositories using either Groovy or Kotlin.
    repositories {
       maven {
           url "https://us-central1-maven.pkg.dev/mobile-sdk-fd2e4/adservr-maven"
       }
    }
    
    repositories {
        maven("https://us-central1-maven.pkg.dev/mobile-sdk-fd2e4/adservr-maven")
    }
    
  2. In the app's build.gradle, declare the com.github.spotim.standalone-ad:1.0.7 dependency in the dependencies block.
    implementation("com.github.spotim:standalone-ad:1.0.7")
    
  3. Sync the project to download the new dependency.


Initialize the SDK

Use the following steps to initialize the SDK:

  1. In the Application class, initialize the SpotIM SDK within onCreate().

    πŸ“˜

    Since onCreate() is the first method called by the application, the SpotIM SDK will be initialized when the application begins.

    class App: Application() {
        override fun onCreate() {
            super.onCreate()
    
    // SDK Initialization
            SpotImAds.init(this, spotId)
        }
    }
    
  2. Set the spot ID.

  3. Build the project.

  4. Run the project.



Add Google Ad Manager

Use the following steps to add Google Ad Manager:

  1. In AndroidManifest.xml, add the Google Mobile Ad app ID inside the <application> element.

    <meta-data
           android:name="com.google.android.gms.ads.APPLICATION_ID"
           android:value="YOUR_APP_ID"/>
    
  2. (Optional) To improve the ad load performance, add the following optimization elements.

    <meta-data
           android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
           android:value="true"/>
    <meta-data
           android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
           android:value="true"/>
    
    


Display an ad

Use the following steps to display an ad:

  1. Add a SpotImAdPlacement to the layout of the app.

    <com.github.spotim.placement.SpotImAdPlacement
        android:id="@+id/ad_placement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
  2. Assign slots to the ad placement using either the programmatic or declarative approach:

    Programmatic Approach

    In the relevant Activity or Fragment class, use setSlot() to assign slots to the ad placement.


    πŸ“˜

    The setSlot method should be added after the layout has been inflated:

    • For an Activity, in onCreate()
    • For a Fragment, in onViewCreated()

    The first setSlot() argument is the row of the ad slot. The second argument is the column of the ad slot.


    val placement = view.findViewById<SpotImAdPlacement>(R.id.ad_placement)
    placement.setSlot(1, 1)
    

    Declarative Approach

    In the XML layout, the slot be set.


    <com.github.spotim.placement.SpotImAdPlacement
           ...
           app:slot_row="1"
           app:slot_column="1"    
    />