Android Standalone Ad
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
Application classUse the following steps to create a class inheriting from the Application class for the later initialization of the SDK:
-
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.
-
In App.kt, define the
Appclass by extending theApplicationclass. TheApplicationclass is the base class for maintaining the global application state.class App : Application() {} -
In the
Appclass, add any needed setup code toonCreate().class App : Application() { override fun onCreate() { super.onCreate() // SDK Initialization will be here } } -
In AndroidManifest.xml, add the
android:name=".App"attribute to direct Android to use your customAppclass as the mainApplicationclass.<?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:
- In AndroidManifest.xml, add the
INTERNETpermission inside the<manifest>element.1. <uses-permission android:name="android.permission.INTERNET" /> - Sync the project.
Add the dependencies
Use the following steps to add the SpotIM SDK to your project
- In the main build.gradle or settings.gradle, add the following Maven repositories using either Groovy or Kotlin.
repositories { ... mavenCentral() }repositories { ... mavenCentral() } - In the app's build.gradle, declare the
io.github.spotim:spotim-sdk-iau:ANDROIDSTANDALONEADVERSIONdependency in thedependenciesblock.implementation("io.github.spotim:spotim-sdk-iau:ANDROIDSTANDALONEADVERSION") - Sync the project to download the new dependency.
Initialize the SDK
Use the following steps to initialize the SDK:
-
In the
Applicationclass, initialize the SpotIM SDK withinonCreate().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) } } -
Set the spot ID.
-
Build the project.
-
Run the project.
Add Google Ad Manager
Use the following steps to add Google Ad Manager:
-
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"/> -
(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:
-
Add a
SpotImAdPlacementto 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"/> -
Assign slots to the ad placement using either the programmatic or declarative approach:
Programmatic Approach
In the relevant
ActivityorFragmentclass, usesetSlot()to assign slots to the ad placement.
The
setSlotmethod should be added after the layout has been inflated:- For an
Activity, inonCreate() - For a
Fragment, inonViewCreated()
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" />
- For an
Updated 7 months ago
