Banner ads are images or text that appear on a portion of an app's screen. The ad stays on screen as the user interacts with the app. The ad can auto refresh periodically.
You'll need to follow the instructions in Getting Started before proceeding with the following integration steps. You'll also find a useful example integration in pubmatic-sample-app included in the SDK directory.
Once you've downloaded and installed the PubMatic Android SDK, use the following sections to implement and configure banner ads:
- Display Banner Ad
- Callback Events Using a Listener
- Set Optional Listeners
- Enable Auto Refresh Functionality
- Multisize Ad Support (Optional)
Display Banner Ad
Use the following steps to include banner ads into your app.
Step 1: Define a banner ad slot in your layout XML
The PubMatic SDK provides the custom view class
PMBannerAdView used to display banner ad creative. Please add the following XML block to your app's
Now you can easily use a reference of
PMBannerAdView in your
Fragment like this:
Optionally, you can also create a
PMBannerAdView instance in the
Fragment context like this:
Step 2: Create Ad Request
PMBannerAdRequest object and set the targeting parameters:
For more targeting parameters, see Passing Targeting Information.
Step 3: Request Ad
loadRequest() from the
Step 4: Deallocate PMBannerAdView
PMBannerAdView instance before your
Fragment is destroyed.
Callback Events Using a Listener
Optionally, you can register a listener to report success and failure callbacks from the ad request:
PMErrorobject used in the
Set Optional Listeners
PubMatic SDK also provides optional listeners that you can register for other callbacks as shown below:
Enable Auto Refresh Functionality
PubMatic Android SDK provides the optional feature to auto refresh a banner ad after a specified interval. You can set the refresh interval in code using
PMBannerAdView.setUpdateInterval(<value in secs>) before calling the loadRequest
()method. Use an interval value within a range of 12 to 120 seconds.
Ad Refresh Behavior for Value of Update Intervals
|Integer value ( i ) in seconds||Banner Auto-refresh behavior|
|i <= 0||Ad will not refresh|
|i > 0 & i <= 12||Ad will refresh after every 12 seconds.|
|i > 12 & i <= 120||Ad will refresh after every i seconds|
|i > 120||Ad will refresh after every 120 seconds|
Multisize Ad Support (Optional)
You can specify multiple sizes in an ad request. Then SSP/Exchange serves the ad with the creative that best fits into those sizes and returns greater CPMs. This flexibility makes a greater number of campaigns/creatives eligible for the current impression, which can lead to increase fill rate.
Get the size of the creative if you need to write your own logic to resize the ad slot. For example: