Android Splash Ad Integration
Overview
Splash ads are full-screen ads displayed when an application starts, with the following characteristics:
- High exposure rate
- Suitable for application startup scenarios
- Strong visual impact
- Higher click-through and conversion rates
Integration Steps
Refer to the SplashAdHelper in the demo example
1. Create Splash Ad Activity
Use the splash ad helper class:
kotlin
/**
* Splash ad helper class
*
* Responsible for splash ad loading, display, and lifecycle management
*
* @param activity Context Activity
* @param logger Log printing tool
*/
class SplashAdHelper(private val activity: Activity, private val logger: PrintLogger) {
/**
* Splash ad object
*
* Used to manage splash ad loading, display, and destruction
*/
private var splashAdObject: SplashAdObject? = null
/**
* Load splash ad
*
* This method creates ad configuration, initializes splash ad object, sets ad listener
* Finally calls load() method to start loading ads
*/
fun load() {
// Create ad configuration object, set placement ID
val adConfig = UjuAdConfig(
placementId = DemoConfig.SPLASH_ID, // Splash ad placement ID
)
// Initialize splash ad object
splashAdObject = SplashAdObject(activity, adConfig)
// Set ad listener to monitor various ad events
splashAdObject?.setAdObjectListener(object : SplashAdObjectListener {
/**
* Ad loaded successfully callback
*
* @param placementId Ad placement ID
*/
override fun onLoadSuccess(placementId: String) {
// Ad loaded successfully, can be displayed now, recommended to check isReady before display
logger.add("Splash: onLoadSuccess")
}
/**
* Ad displayed successfully callback
*/
override fun onAdShow() {
// Get ad information, record ecpm value
val adInfo = splashAdObject?.getAdInfo()
logger.add("Splash: onAdShow,ecpm:${adInfo?.ecpm}")
}
/**
* Ad display error callback
*
* @param error Error information
* @param placementId Ad placement ID
*/
override fun onAdError(
error: UjuException,
placementId: String
) {
// Record ad error information
logger.add("Splash: onAdError:${error.message}")
DemoLogUtils.e("onAdError:${error.message}")
}
/**
* Ad clicked callback
*/
override fun onAdClicked() {
// Record ad click event
logger.add("Splash: onAdClicked")
}
/**
* Ad closed callback
*/
override fun onAdClosed() {
// Record ad close event
logger.add("Splash: onAdClosed")
// Destroy ad object, release resources
splashAdObject?.destroy()
splashAdObject = null
}
/**
* Ad loading failed callback
*
* @param error Error information
* @param placementId Ad placement ID
*/
override fun onLoadError(
error: UjuException,
placementId: String
) {
// Record loading error information
logger.add("Splash: onLoadError: ${error.message}")
DemoLogUtils.e("onLoadError:${error.message}")
}
})
// Start loading ad
splashAdObject?.load()
// Record ad loading event
logger.add("Splash: load, placementId:${adConfig.placementId}")
DemoLogUtils.d("Splash: load, placementId:${adConfig.placementId}")
}
/**
* Display splash ad
*
* Before displaying the ad, it checks if the ad is ready
* Only when the ad status is ready, it calls show() method to display the ad
*
* @param viewGroup ViewGroup for containing the ad
*/
fun show(viewGroup: ViewGroup) {
// Check if ad is ready
if (splashAdObject?.isReady() == true) {
// Ad is ready, display ad
splashAdObject?.show(activity, viewGroup)
} else {
// Ad is not ready, record log
logger.add("Splash: Ad not ready yet")
}
}
/**
* Check if ad is loaded
*
* @return true if ad object is created, false otherwise
* Note: This method only checks if ad object exists, does not guarantee ad is ready
* To check if ad can be displayed, use isReady() method
*/
fun isLoaded(): Boolean {
return splashAdObject != null
}
/**
* Destroy ad object
*
* When ad is no longer needed, call this method to destroy ad object and release resources
*/
fun destroy() {
// Destroy ad object
splashAdObject?.destroy()
// Set ad object reference to null
splashAdObject = null
}
}2. Create Splash Ad Layout
Create activity_splash.xml layout file:
xml
<!-- activity_splash.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_background">
<!-- App logo -->
<ImageView
android:id="@+id/app_logo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:src="@drawable/app_logo"
android:visibility="visible"/>
<!-- Ad container -->
<FrameLayout
android:id="@+id/splash_ad_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
<!-- Skip button -->
<Button
android:id="@+id/skip_button"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:background="@drawable/skip_button_bg"
android:text="Skip"
android:textColor="@android:color/white"
android:textSize="14sp"
android:paddingHorizontal="16dp"
android:onClick="onSkipClicked"/>
</RelativeLayout>3. Configure AndroidManifest.xml
Register the splash ad Activity in AndroidManifest.xml:
xml
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme"/>4. Create Style File
Create splash theme in styles.xml:
xml
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>Advanced Configuration
1. Custom Skip Button
kotlin
// Custom skip button
val skipButton = findViewById<Button>(R.id.skip_button)
skipButton.setOnClickListener {
// Jump to main page
gotoMainActivity()
}2. Load Parameters
kotlin
// In the load method of SplashAdHelper, you can add extra parameters through adConfig
val adConfig = UjuAdConfig(
placementId = DemoConfig.SPLASH_ID,
// Add extra load parameters
extraParams = mapOf(
"app_version" to "1.0.0",
"device_type" to "phone"
)
)3. Ad Timeout Handling
kotlin
// Handle ad timeout in SplashAdHelper's setAdObjectListener
override fun onAdError(error: UjuException, placementId: String) {
logger.add("Splash: onAdError:${error.message}")
DemoLogUtils.e("onAdError:${error.message}")
// Ad error handling, including timeout
gotoMainActivity()
}
// Or set timeout handling in Activity
private val SPLASH_TIMEOUT = 3000L // 3 seconds timeout
// Set timeout task in onCreate
handler.postDelayed({
// Timeout handling
gotoMainActivity()
}, SPLASH_TIMEOUT)Best Practices
1. Splash Ad Optimization
- Set reasonable timeout: Recommended 3-5 seconds
- Provide skip button: Allow users to skip ads
- Preload strategy: Start loading ads before app startup
- Failure handling: Ensure app can normally enter main page when ad loading fails
2. User Experience Optimization
- Maintain brand consistency: Splash ad interface should be consistent with app style
- Avoid over-marketing: Don't display too many promotional messages in splash ads
- Optimize loading speed: Ensure splash ads can load and display quickly
- Frequency control: Reasonably control the display frequency of splash ads
3. Avoidable Issues
- Don't set too long ad timeout
- Don't display content unrelated to the app in splash ads
- Don't affect normal app startup speed
- Don't add complex interactions in splash ads
Common Questions
Q: Why isn't the splash ad displaying?
A: Possible reasons:
- Incorrect ad unit ID
- Network connection issues
- Insufficient ad inventory
- Ad loading timeout
- Device restrictions
Q: How to improve splash ad effectiveness?
A: Recommendations:
- Design attractive splash interface
- Choose appropriate ad materials
- Optimize ad loading and display strategies
- Ensure ads are relevant to app content
- Increase app user activity
Q: Can splash ads be used in Fragment?
A: Not recommended, splash ads should be used in independent Activity, which can:
- Better control app startup process
- Avoid interaction conflicts with other interfaces
- Improve ad display success rate
Code Example
Complete Splash Ad Integration Example
Using the above SplashAdHelper class, you can easily integrate splash ads in your splash Activity:
kotlin
class SplashActivity : AppCompatActivity() {
private lateinit var splashAdHelper: SplashAdHelper
private val SPLASH_DISPLAY_TIME = 3000 // 3 seconds
private lateinit var handler: Handler
private lateinit var gotoMainRunnable: Runnable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
handler = Handler(Looper.getMainLooper())
gotoMainRunnable = { gotoMainActivity() }
// Initialize splash ad helper class
splashAdHelper = SplashAdHelper(this, object : PrintLogger {
override fun add(message: String) {
Log.d("SplashAd", message)
}
})
// Load splash ad
splashAdHelper.load()
// Delay displaying ad to ensure ad is loaded
handler.postDelayed({ showSplashAd() }, 500)
}
private fun showSplashAd() {
// Get ad container
val adContainer = findViewById<ViewGroup>(R.id.splash_ad_container)
// Display ad
splashAdHelper.show(adContainer)
// Set skip button click event
findViewById<Button>(R.id.skip_button).setOnClickListener {
// Jump to main page
gotoMainActivity()
}
}
private fun gotoMainActivity() {
// Remove delayed task
handler.removeCallbacks(gotoMainRunnable)
// Jump to main page
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
override fun onDestroy() {
super.onDestroy()
// Clean up resources
handler.removeCallbacks(gotoMainRunnable)
splashAdHelper.destroy()
}
}