Using Banners with Android

We do not serve banner ads to apps that are opted-out of behavioral targeting.
Banner ads are limited to Android SDKs version 8.0.1 and above.
CharboostBanner is a View subclass able to show banner ads. Unlike other ad types, the developer is responsible for integrating the banner with the app UI and presenting it to the user.
Multiple Banners can be displayed at once if desired.
Creating a banner
XML
To create a new banner it is possible to do it in the xml layout file.
<com.chartboost.sdk.ChartboostBanner
​           xmlns:​chartboost​=​"h​ ttp://schemas.android.com/apk/res-auto​"
           android:​id​=​"@+id/example_banner"
           android:​layout_width​=​"wrap_content"
           android:​layout_height​="​ wrap_content"
           android:​layout_centerInParent​=​"true"
           chartboost:​location​=​"start"
           chartboost:​size​=​"STANDARD"​ />
The developer is responsible for defining unique location names and using one of the three provided sizes and visual placement in the layout.
Programmatically
The developer needs to create ChartboostBanner object and attach it to the view. View can be customised with LayoutParams. BannerSize object corresponds to the XML size values ChartboostBannerListener can be attached later with setListener function.
//create new banner with:
// - context - required
// - location - required
// - BannerSize - Required
// - ChartboostBannerListener - Optional
ChartboostBanner chartboostBanner = new ChartboostBanner(this, bannerLocation, bannerSize, null);

//Attach object to layout
mBannerContainer.removeAllViews();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)​;
params.gravity = CENTER_HORIZONTAL;
mBannerContainer.addView(chartboostBanner, params);
The developer can detach Banner from the view and destroy the object. In order to do so, use:
chartboostBanner​.detachBanner​();
Showing banner ads
To make the banner show an ad first, create and present it as seen before, and then call this method:
 
to cache the ad
chartboostBanner​​.cache​();
and show to make the ad visible
chartboostBanner​.show​();
Banner ads sizes
STANDARD – 320 x 50
MEDIUM – 300 x 250
LEADERBOARD – 728 x 90
Auto refreshing
By default a banner will automatically update its content on its own. The refresh default rate is 30s. This means you only need to call bellow method once and the banner will get new ads and show them by itself, gracefully handling errors if they occur. Banners will also be refreshed once clicked.
chartboostBanner.setAutomaticallyRefreshesContent(​true​);
Manually handling banner refresh
If you decide to disable banner auto-refreshing you can do that by setting
chartboostBanner.setAutomaticallyRefreshesContent(​false​);
In this case you’ll need to call the cache function to load an ad and implement the corresponding listener method to be notified of when the ad is ready to be shown. Example:
void cacheBanner​()​ {
    chartboostBanner.cache();
}

@Override
void onAdCached​(ChartboostCacheEvent event, ChartboostCacheError error) {
    ​if​(error != ​null​) {
        chartboostBanner.show() ;
    }
}

@Override
​​void onAdShown​(ChartboostShowEvent event, ChartboostShowError error)​ {​
    if​(error != ​null​) {
        chartboostBanner.cache();
    }
}
Banner listener
Every banner has an optional listener which can be provided when initialized or assigned afterwards.
Implementing listener methods is the way you can be notified and respond to events related to the banner life-cycle.
These are all the listener methods:
 
Class name: ChartboostBannerListener
// Called after ad is cached and ready to be shown or indicated reason of failure
void onAdCached​(ChartboostCacheEvent event, ChartboostCacheError error);

// Called after ad is displayed and visible on the screen or indicated reason of failure
void ​​onAdShown​(ChartboostShowEvent event, ChartboostShowError error);

// Called after ad has been clicked or indicated reason of failure
void onAdClicked​(ChartboostClickEvent event, ChartboostClickError error);
  • ChartboostCacheEvent​, ChartboostShowEven​, ChartboostClickEvent​ – object is not null when event performed without any issues.
  • ChartboostCacheError​,​ ChartboostShowError​,​ ChartboostClickError​​ – error object should be null unless there was an issue. The developer should check for both event and error object to determine if flow was successful.
Public Banner Errors
All errors can be checked in the public code and to check actual error code value, error can be accessed like this:
ChartboostCacheError cacheError = new ChartboostCacheError(ChartboostCacheError.Code.INTERNAL);
cacheError.code.getErrorCode()

ChartboostShowError showError = new ChartboostCacheError(ChartboostShowError.Code.INTERNAL);
showError.code.getErrorCode()

ChartboostClickError clickError = new ChartboostClickError(ChartboostClickError.Code.INTERNAL);
clickError.code.getErrorCode()