Banner Ads | Android

This guide will show you how to set up banner ad views for the Android SDK.

Adding a BannerView

Inside your activity and within onCreate(), create your BannerView.

BannerView mBanner = new BannerView (context);

The SDK supports both ActivityContext and ApplicationContext.

Now your banner has been created, but it will not be printed to the screen yet. You will now need to attach it:

  • First, you need to get an instance from your layout using:
RelativeLayout myRelativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayoutId);
  • Next, attach the banner view to your layout, by using the following:
myRelativeLayout.addView(mBanner, new LayoutParams(LayoutParams.MATCH_PARENT, 70));

Now you are done, and the banner is attached to your layout, The next step is to set your IDs and to start requesting banners.

Requesting Banner Ads

In order to fetch live ads inside the activity, add your PublisherID and AdspaceID in the onCreate method. For testing purposes, we highly recommend using publisherId(0) and adSpaceId(0). For example:

mBanner.getAdSettings().setPublisherId(publisherId);
mBanner.getAdSettings().setAdspaceId(adspaceId);
mBanner.asyncLoadNewBanner();

Activate GPS Location

Enriching traffic with location data almost always guarantees higher eCPMs. To activate the location parameter, you have two options:

Option 1: Automatic Location Tracking

You can use automatic location tracking by calling the following method: (This allows the SOMA lib to update the user’s location automatically).

mBanner.setLocationUpdateEnabled(true);

Option 2: Location Information

If your application already gathers location information, you can also use that information by calling:

mBanner.getUserSettings().setLatitude(37.331689);
mBanner.getUserSettings().setLongitude(-122.030731);

Using Targeting Data

The BannerView allows developers to set the following metadata:

KeywordSearch:

Tags set by the app developer that describes the type of content (e.g., cars, news, restaurants, New York…)

Setting a Keyword List:

mBanner.getUserSettings().setKeywordList(“Android,California”); //connect different terms with “,”

QuerySearch:

A search term or keywords entered by the user (e.g., restaurant, New York)

Example: if there is a search bar in your app and an end user searches for “restaurant,” you can pass the query string to search the terms.

Setting a search term:

mBanner.getUserSettings().setSearchQuery(“red car,mini”); //connect different terms with “,”

Age, gender:

 The age and gender of the user (e.g., gathered through user registration)

Setting the user’s age:

mBanner.getUserSettings().setAge(35); //available parameters are integers.

Setting the user’s gender:

mBanner. getUserSettings().setUserGender(UserSettings.Gender.MALE); //available parameters are MALE and FEMALE.

Enabling and Disabling COPPA

You are able to activate and deactivate COPPA (Children’s Online Privacy Protection Act) by using the following:

mBanner.getUserSettings().setCOPPA(value); // true to enable, false otherwise.

BannerView Listeners

BannerView allows you to use listeners to be notified when a banner status changes. There are two kinds of BannerView listeners:

  • The first is an ad listener that will get notified as soon as a banner download was completed (Success or Error).
mBanner.addAdListener(new AdListenerInterface() {
   @Override
    public void onReceiveAd(AdDownloaderInterface adDownloader, ReceivedBannerInterface banner) {
        if(banner.getStatus() == BannerStatus.ERROR){
            Log.w(“”+banner.getErrorCode(), “”+banner.getErrorMessage());
        } else {
            // Banner download succeeded
}}});
  • The second is the banner state listener, which gets notified when a banner opens or closes a landing page.
mBanner.setBannerStateListener(new BannerStateListener() {
    @Override
    public void onWillOpenLandingPage(BannerView bannerView) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onWillCloseLandingPage(BannerView bannerView) {
        // TODO Auto-generated method stub
    }
});

Destroying the BannerView

After you finish using the BannerView, it is highly recommended to clean up the memory, please call the destroy() method as per the following example:

@Override
public void onDestroy(){
    if(mBannerView!=null) {
        mBannerView.destroy();
    }
    super.onDestroy();
}

Banner Auto Reload

The Banner View is the only banner type which uses an auto-reload system. The auto-refresh timer is automatically deactivated as soon as the banner is off-screen.

The auto-reload frequency is by default set to 60 seconds. You can modify this frequency using the following method:

mBanner.setAutoReloadFrequency(30); // parameter is in seconds between 10 and 240

If you wish to disable the auto-reload frequency, please use the following method:

mBanner.setAutoReloadEnabled(false);

Banner Dimensions

You use the following method for specifying a desired banner ad dimension:

mBanner.getAdSettings().setAdDimension(AdDimension.MEDIUMRECTANGLE);
Available Dimension Values:
  • DEFAULT (320 x 50)
  • LEADERBOARD (728 x 90)
  • MEDIUM RECTANGLE (300 x 250)
  • SKYSCRAPER (120 x 600)

Change Background Color

By default, the BannerView is set to a white background but you can change to your preferred background color using the following method:

mBanner.setBackgroundColor(Color.BLACK);

Create a Banner in your XML layout

In your XML layout you can directly declare a banner view, for example:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:BannerView=”http://schemas.android.com/apk/res-auto”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent” >
    <com.smaato.soma.BannerView
        android:id=”@+id/bannerView”
        android:layout_width=”match_parent”
        android:layout_height=”50dp”
        BannerView:adSpaceId=”yourAdSpaceId”
        BannerView:publisherId=”yourPublisherId”
        BannerView:loadNewBanner=”true” />
</RelativeLayout>

Available Parameters For Banners

Additional Parameters that can be set Value Range Description
BannerView:adDimension=”default” String (“default”, “mediumrectangle”, “leaderboard”, “skyscraper”) Set requested ad dimension.
BannerView:adSpaceId=”0″ Integer Your ad-space-ID
BannerView:publisherId=”0″ Integer Your publisher-ID
BannerView:autoReloadEnabled=”false” Boolean Enable/disable auto-reload
BannerView:autoReloadFrequency=”60″ Integer Set the auto reload frequency. Minimum = 10sec. Maximum = 240sec.
BannerView:backgroundColor=”#FF0101″ Color Set the background color.
BannerView:age=”30″ Integer The user’s age
BannerView:gender=”m” String (“f”, “m”) The user’s gender.
BannerView:keywordList=”Car, Racing, Game” String Keywords, describing the content. Comma separated.
BannerView:latitude=”37.787827″   Latitude of user’s location.
BannerView:longitude=”-122.406728″   Longitude of user’s location.
BannerView:City=”Marseille” String City of user’s location
BannerView:region=”Provence” String Region of user’s location
BannerView:country=”France” String Country of user’s location
BannerView:locationUpdateEnabled=”true” Boolean Enable transmission of location data.
BannerView:loadNewBanner=”true” Boolean If set, automatically makes an ad request once the layout is created. Must be the last attribute set!

Modified: July 18, 2019 at 2:56 pm