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
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Ad Sizes
UJU SDK supports the following banner ad sizes:
| Size Type | Width x Height | Description | Suitable Scenarios |
|---|---|---|---|
| BANNER | 320x50 | Standard banner | Mobile applications |
| LARGE_BANNER | 320x100 | Large banner | Need more display space |
| MEDIUM_RECTANGLE | 300x250 | Medium rectangle | Content pages |
| FULL_BANNER | 468x60 | Full-size banner | Tablet devices |
| LEADERBOARD | 728x90 | Leaderboard banner | Tablet or desktop devices |
| SMART_BANNER | Adaptive | Smart banner | Automatically 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
