Banner Ads
Overview
Before You Start. Make sure that you have correctly integrated the BlueStack SDK into your application. Integration is outlined here.
Create a BannerView
To create a banner you have to init an object with type BannerView.
- Programmatic
- LayoutEditor
- Java
- Kotlin
// Create a new bannerView.
BannerView bannerView = new BannerView(getActivity());
bannerView.setPlacementId("Placement_ID")
bannerView.setAdSize(AdSize.BANNER)
// Add new bannerView into adLayout.
adLayout.removeAllViews();
adLayout.addView(bannerView);
// Create a new bannerView.
val bannerView = BannerView(requireActivity())
bannerView.setPlacementId("Placement_ID")
bannerView.setAdSize(AdSize.BANNER)
// Add new bannerView into adLayout.
adLayout.removeAllViews()
adLayout.addView(bannerView)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.azerion.bluestack.banner.BannerView
...
app:adSize="BANNER"
app:placementId="/5180317/banner"
...
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Note: if your application support different screen sizes on Tablet and Phone, it is better to use following AdSize:
- Java
- Kotlin
AdSize adSize = getResources().getBoolean(R.bool.is_tablet) ? AdSize.DYNAMIC_LEADERBOARD : AdSize.DYNAMIC_BANNER;
bannerView.setAdSize(adSize)
val adSize = resources.getBoolean(R.bool.is_tablet) ? AdSize.DYNAMIC_LEADERBOARD : AdSize.DYNAMIC_BANNER
bannerView.setAdSize(adSize)
Load a Banner Ad
Note: Make all calls to the BlueStack SDK on the main thread.
With RequestOptions
To request an Banner ad using RequestOptions, provide an instance of RequestOptions in the BannerView's load method:
- Java
- Kotlin
bannerView.load(requestOptions)
bannerView.load(requestOptions)
Without RequestOptions
- Java
- Kotlin
bannerView.load()
bannerView.load()
Ad events
Register for banner events
To recieve ad's lifecycle events regisger a listerner in BannerView
.
- Java
- Kotlin
public class BannerAdFragment extends Fragment implements BannerViewListener {
...
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
bannerView = new BannerView(requireActivity());
bannerView.setBannerViewListener(this);
..
}
...
}
class BannerAdFragment : Fragment(), BannerViewListener {
...
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
bannerView = BannerView(requireActivity())
bannerView.setBannerViewListener(this)
...
}
...
}
Implement banner events
The SDK will notify your Listener of all possible events listed below :
- onAdLoad(): will be called by the SDK when banner ad finishes loading.
- Java
- Kotlin
@Override
public void onAdLoad(int preferredHeightDP){
Log.d(TAG, "your banner is ready");
...
// it's preferable to adjust the ad container view size to match the returned Ad size for a better display
...
}
override fun onAdLoad(preferredHeightDP: Int){
Log.d(TAG, "your banner is ready")
...
// it's preferable to adjust the ad container view size to match the returned Ad size for a better display
...
}
- onAdFailToLoad(): will be called when all ads servers fail. it will return the error of last called ads server.
- Java
- Kotlin
@Override
public void onAdFailToLoad(Exception exception) {
Log.e(TAG, "banner did fail :" + exception);
}
override fun onAdFailToLoad(exception: Exception) {
Log.e(TAG, "banner did fail :$exception")
}
- onResize() : will be called when the banner has changed size
- Java
- Kotlin
@Override
public void onResize(Size size) {
...
// it's preferable to adjust the ad container view size to match the returned Ad size for a better display
Log.d(TAG, "Banner did resize w dp " + size.getWidth() + " h dp " + size.getHeight());
...
}
override fun onResize(size: Size) {
...
// it's preferable to adjust the ad container view size to match the returned Ad size for a better display
Log.d(TAG, "Banner did resize w dp " + size.width + " h dp " + size.height)
...
}
- onAdRefresh() : will be called when the banner has refreshed
- Java
- Kotlin
@Override
public void onAdRefresh() {
Log.d(TAG, "banner refresh succeed")
}
override fun onAdRefresh() {
Log.d(TAG, "banner refresh succeed")
}
- onAdFailToRefresh() : will be called when the banner fail to refresh.
- Java
- Kotlin
@Override
public void onAdFailToRefresh(Exception exception) {
Log.e(TAG, "banner did fail to refresh :" + exception);
}
override fun onAdFailToRefresh(exception: Exception) {
Log.e(TAG, "banner did fail to refresh :$exception")
}
- onAdClicked(): will be called when user click the banner ad.
- Java
- Kotlin
@Override
public void onAdClicked() {
Log.d(TAG, "Ad Clicked");
}
override fun onAdClicked() {
Log.d(TAG, "Ad Clicked")
}
Destroying Banner Ad
When you have finished your ads plant you must free the memory.
- Java
- Kotlin
@Override
protected void onDestroy() {
bannerView.destroy();
bannerView = null;
super.onDestroy();
}
override fun onDestroy() {
bannerView.destroy()
bannerView = null
super.onDestroy()
}
AdSize
BlueStack ads provides variant pre-defined sizes, See table below for details about our supported standard banner sizes:
MNGAdSize | Description | Dimensions in dp (WxH) |
---|---|---|
BANNER | Standard Banner | 320 x 50 |
LARGE_BANNER | Large Banner | 320 x 100 |
MEDIUM_RECTANGLE | Medium Rectangular Banner | 300 x 250 |
DYNAMIC_BANNER | Adjusted Banner | Screen width x 50 |
FULL_BANNER | Full Banner | 468 x 60 |
LEADERBOARD | Standard Banner for tablet | 728 x 90 |
DYNAMIC_LEADERBOARD | Adjusted Banner for tablet | Screen width x 90 |
Adapt the banner size after loading
You can resize the Banner View height to match the creative's width/height ratio, this is often the case when your Banner View needs to deliver view over 50 dp. (This does not happen when setting your view as wrap_content)
Here's an example:
XML Code :
<LinearLayout
android:id="@+id/bannerContainer"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"/>
- Java
- Kotlin
@Override
public void onResize(Size size) {
// convert Dp To Pixel
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float bannerPreferredHeightPx = size.getHeight() * (metrics.densityDpi / 160f);
// adapt the banner size
bannerContainer.getLayoutParams().height = bannerPreferredHeightPx;
bannerContainer.requestLayout();
}
OR
@Override
public void onAdLoad(int preferredHeightDP) {
// convert Dp To Pixel
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float bannerPreferredHeightPx = preferredHeightDP * (metrics.densityDpi / 160f);
// adapt the banner size
bannerContainer.getLayoutParams().height = bannerPreferredHeightPx;
bannerContainer.requestLayout();
}
override fun onResize(size: Size) {
// convert Dp To Pixel
val resources = context.resources
val metrics: DisplayMetrics = resources.displayMetrics
val bannerPreferredHeightPx = size.height * (metrics.densityDpi / 160f)
// adapt the banner size
bannerContainer.layoutParams.height = bannerPreferredHeightPx
bannerContainer.requestLayout()
}
OR
override fun onAdLoad(preferredHeightDP: Int) {
// convert Dp To Pixel
val resources = context.resources
val metrics: DisplayMetrics = resources.displayMetrics
val bannerPreferredHeightPx = preferredHeightDP * (metrics.densityDpi / 160f)
// adapt the banner size
bannerContainer.layoutParams.height = bannerPreferredHeightPx
bannerContainer.requestLayout()
}
Example
Example | Description |
---|---|
Banner - (50dp or 90dp ) | A banner is a small bar ad that appears at the bottom or top of your content. Usually sized 320 x 50. Only include one ad per page or show one ad at a time if scrolling. In all cases, the banner width is flexible with a minimum of 320px.. If you are building your app for iPad consider using 90px and 50px for iphone. |
Square - Medium rectangle (300 x 250) | Square banner also known as a medium rectangle (300 x 250). This format can increase earnings when both text and image ads are enabled. Performs well when embedded within text content or at the end of articles. |