Skip to content

Android Banner Ad Integration

Overview

Banner ads are rectangular ads displayed at the top or bottom of the application interface, with the following characteristics:

  • Small space occupation, no impact on user experience
  • Continuous display, increasing ad exposure
  • Fast loading speed, quick response
  • Suitable for use in various application scenarios

Integration Steps

Refer to the BannerAdHelper in the demo example

1. Initialize Ad

Load banner ads in Activity or Fragment:

kotlin
/**
 * Banner ad helper class
 * 
 * Responsible for banner ad loading, display, and lifecycle management
 * 
 * @param activity Context Activity
 * @param logger Log printing tool
 */
class BannerAdHelper(private val activity: Activity, private val logger: PrintLogger) {

    /**
     * Banner ad object
     * 
     * Used to manage banner ad loading, display, and destruction
     */
    private var bannerAdObject: BannerAdObject? = null

    /**
     * Load banner ad
     * 
     * This method creates ad configuration, initializes banner ad object, sets ad listener
     * Finally calls load() method to start loading ads
     */
    fun load() {
        // Create ad configuration object, set placement ID and ad size
        val adConfig = UjuAdConfig(
            placementId = DemoConfig.BANNER_ID, // Banner ad placement ID
            adViewSize = AdViewSize(width = 320, height = 100) // Ad size: 320x100
        )
        
        // Initialize banner ad object
        bannerAdObject = BannerAdObject(activity, adConfig)
        
        // Set ad listener to monitor various ad events
        bannerAdObject?.setAdObjectListener(object : FeedAdObjectListener {
            /**
             * 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("Banner: onLoadSuccess")
            }

            /**
             * 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("Banner: onLoadError:${error.message}")
                DemoLogUtils.e("onLoadError:${error.message}")
            }

            /**
             * Ad display error callback
             * 
             * @param error Error information
             * @param placementId Ad placement ID
             */
            override fun onAdError(
                error: UjuException,
                placementId: String
            ) {
                // Record ad display error information
                logger.add("Banner: onAdError:${error.message}")
                DemoLogUtils.e("onAdError:${error.message}")
            }

            /**
             * Ad displayed successfully callback
             */
            override fun onAdShow() {
                // Get ad information, record ecpm value
                val adInfo = bannerAdObject?.getAdInfo()
                logger.add("Banner: onAdShow:ecpm:${adInfo?.ecpm}")
            }

            /**
             * Ad clicked callback
             */
            override fun onAdClicked() {
                // Record ad click event
                logger.add("Banner: onAdClicked")
            }

            /**
             * Ad closed callback
             */
            override fun onAdClosed() {
                // Record ad close event
                logger.add("Banner: onAdClosed")
            }

            /**
             * Ad landing page closed callback
             */
            override fun onLpClosed() {
                // Record landing page close event
                logger.add("Banner: onLpClosed")
            }
        })
        
        // Start loading ad
        bannerAdObject?.load()
        // Record ad loading event
        logger.add("Banner: load, placementId:${adConfig.placementId}")
        DemoLogUtils.d("Banner: load, placementId:${adConfig.placementId}")
    }

    /**
     * Display banner 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 (bannerAdObject?.isReady() == true) {
            // Ad is ready, display ad in specified ViewGroup
            bannerAdObject?.show(activity, viewGroup)
        } else {
            // Ad is not ready, record log
            logger.add("Banner: 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 bannerAdObject != 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
        bannerAdObject?.destroy()
        // Set ad object reference to null
        bannerAdObject = null
    }
}

Ad Sizes

UJU SDK supports the following banner ad sizes:

Size TypeWidth x HeightDescriptionSuitable Scenarios
BANNER320x50Standard bannerMobile applications
LARGE_BANNER320x100Large bannerNeed more display space
MEDIUM_RECTANGLE300x250Medium rectangleContent pages
FULL_BANNER468x60Full-size bannerTablet devices
LEADERBOARD728x90Leaderboard bannerTablet or desktop devices
SMART_BANNERAdaptiveSmart bannerAutomatically adapts to screen width

Best Practices

1. Ad Placement

  • Bottom placement: Most common position, does not block main content
  • Top placement: Suitable for certain specific application scenarios
  • Between content: Inserted in long articles or lists

2. Optimization Suggestions

  • Reasonable refresh interval: Recommended 30-60 seconds, avoid too frequent refresh
  • Preload ads: Load ads in advance before they need to be displayed
  • Handle network status: Adjust loading strategy appropriately when network is poor
  • Test different sizes: Choose appropriate ad size according to application interface

3. Avoidable Issues

  • Don't place multiple banner ads on the same screen
  • Don't block core functions of the application
  • Don't place ads in areas with frequent user interactions
  • Don't set too short refresh intervals

Common Questions

Q: Why isn't the banner ad displaying?

A: Possible reasons:

  • Incorrect placement ID
  • Network connection issues
  • Insufficient ad inventory
  • Layout issues (container is invisible or size is 0)
  • Improper refresh interval settings

Q: How to improve banner ad click-through rate?

A: Recommendations:

  • Choose appropriate ad placement
  • Ensure ads are relevant to application content
  • Use appropriate ad sizes
  • Avoid ad occlusion
  • Optimize application user experience

Q: Can banner ads be used in RecyclerView?

A: Yes, but need to pay attention to:

  • Manage ad views in ViewHolder
  • Correctly handle ad lifecycle
  • Avoid frequent creation and destruction of ad views