Banner Ad
This article will go through all the steps required to display an Banner Ad in your application.
Banner Ads are small or medium rectangle ads that have specific dimensions and occupy a certain position of the screen width and they can be freely integrated within the content of the app.
The banner comes with two predefined dimensions:
- small banner (320x50)
- MPU (300x250)
You have registered an application on your Ogury Dashboard. If not, please refer to the Getting Started section before the next steps.
- Click on the Monetization settings in the left-menu.
- Copy the Ad unit id as you will need this information later to finish the integration.
In all the following code samples, we will refer to this Ad unit id by using the string
YOUR_AD_UNIT_ID
.The Ogury SDK provides the
OguryBannerAdView
view that lets you load, display and control your Banner Ad. The OguryBannerAdView
can either be integrated:- programmatically from you code.
- in your layout.
Programmatically
In layout
- Declare an
OguryBannerAdView
instance variable in theActivity
where you want to display a Banner Ad:
private OguryBannerAdView banner;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OguryBannerAdView banner = new OguryBannerAdView(context);
}
OguryBannerAdView
takes the following parameter:banner.setAdUnit("YOUR_AD_UNIT_ID");
The
setAdUnit
method takes the following parameter:- the Ad unit id of the Banner Ad. If you do not have one yet, you can refer to the first step to create it.
banner.setAdSize(OguryBannerAdSize.SMALL_BANNER_320x50);
The
setAdSize
method takes the following parameter:- the size of the Banner Ad. It can take one of the value defined in
OguryBannerAdSize
object:
OguryBannerAdSize value | Definition |
SMALL_BANNER_320x50 | Small Banner Ad (320x50) |
MPU_300x250 | Mid Page Unit Banner Ad (300x250) |
To start loading an ad, call the
loadAd
method:banner.loadAd();
Since it may take a few seconds to fetch the ad resources (video, image, ...) from the network, you should call the
loadAd
method as soon as possible.Ideally, you should implement one of the two following examples depending on your use case:
public class MyActivity extends Activity {
private OguryBannerAdView banner;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OguryConfiguration.Builder oguryConfigurationBuilder = new OguryConfiguration.Builder(this, "OGY-XXXXXXXXXXXX");
Ogury.start(oguryConfigurationBuilder.build());
banner = new OguryBannerAdView(context);
banner.setAdUnit("YOUR_AD_UNIT_ID");
banner.setAdSize(OguryBannerAdSize.SMALL_BANNER_320x50);
banner.loadAd();
}
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
banner = new OguryBannerAdView(context);
banner.setAdUnit("YOUR_AD_UNIT_ID");
banner.setAdSize(OguryBannerAdSize.SMALL_BANNER_320x50);
banner.loadAd();
}
After the banner is loaded, you can add it to the view:
ViewGroup bannerContainer = findViewById(R.id.banner_container);
bannerContainer.addView(banner);
Add
OguryBannerAdView
it in the layout of an Activity
or Fragment
.<com.ogury.ed.OguryBannerAdView
android:id="@+id/banner_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:adUnit="YOUR_AD_UNIT_ID"
app:bannerAdSize="SMALL_BANNER_320x50" />
- The
adUnit
attribute should contain the Ad unit id of the Banner Ad. If you do not have one yet, you can refer to the first step to create it. - The
bannerAdSize
attribute can be:
Banner Ad size | Definition |
"SMALL_BANNER_320x50" | Small Banner Ad (320x50) |
"MPU_300x250" | Mid Page Unit Banner Ad (300x250) |
The
adUnit
and bannerAdSize
attributes are optional and they can be set programatically.The second step after adding the banner in the layout is to get hold of the banner in the
Activity
or Fragment
. To start loading an ad, call the loadAd
method:OguryBannerAdView banner = findViewById(R.id.banner_view);
banner.loadAd();
Ideally, you should implement one of the two following examples depending on your use case:
public class MyActivity extends Activity {
private OguryBannerAdView banner;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OguryConfiguration.Builder oguryConfigurationBuilder = new OguryConfiguration.Builder(this, "OGY-XXXXXXXXXXXX");
Ogury.start(oguryConfigurationBuilder.build());
banner = findViewById(R.id.banner_view);
banner.loadAd();
}
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
banner = findViewById(R.id.banner_view);
banner.loadAd();
}
As our algorithm works with personified targeting, you may not receive any ad while testing your application.
You can test your integration by adding the suffix
_test
to your interstitial ad unit id, for more details go to Test your implementation page. Note that if you have just registered your application in the Ogury Dashboard, it will take around 15 minutes until you can successfully load an ad.
If you are not able to display any Banner Ad, we recommend you to log every callbacks of the
OguryBannerAdListener
to follow the lifecycle of the Banner Ad. If you encounter the onAdError
callback, you can check the Error codes section below to understand the error and get some advice on how to solve the issue.Additionally, you can follow the integration logs to get useful information on your integration status and any issue that might occur when displaying ads.
The Banner Ad is automatically destroyed when your
Activity
or Fragment
is destroyed. To destroy the Banner Ad, call the destroy
method:banner.destroy();
The Ogury SDK provides the
OguryBannerAdListener
to listen to the lifecycle of an Banner Ad instance. To register an
OguryBannerAdListener
, add the following code just after instantiating the OguryBannerAdView
:banner.setListener(new OguryBannerAdListener() {
// ...
});
The
OguryBannerAdListener
exposes the following methods:Methods | Definition |
onAdLoaded | The SDK is ready to display the ad provided by the ad server. |
onAdDisplayed | The ad has been displayed on the screen. |
onAdClicked | The user clicked on the Ad. |
onAdClosed | The ad has been closed by the user. |
onAdError | The ad failed to load or display. The oguryError parameter contains the reason of the failure. All error codes are detailed in the section below. |
The Ogury SDK also provides the
OguryAdImpressionListener
interface to listen and count the ad impressions. To register an
OguryAdImpressionListener
, add the following code before calling the loadAd
method:banner.setAdImpressionListener(new OguryAdImpressionListener() {
@Override public void onAdImpression() {
// count an impression
}
});
The
OguryAdImpressionListener
exposes the following method:Methods | Definition |
onAdImpression | The ad has triggered an impression. |
When an ad fails to load or to be displayed, the SDK calls the
onAdError
callback with an OguryError
object. The OguryError
object contains the cause of the failure. You can get the error code by calling the getErrorCode
method and a more explicit message by calling the getMessage
method.The error codes that you can encounter are defined in the
OguryAdFormatErrorCode
object:Name | Value | Definition |
NO_INTERNET_CONNECTION | 0 | No Internet connection The device has no Internet connection. Try again once the device is connected to the Internet. |
LOAD_FAILED | 2000 | Load failed The ad failed to load for an unknown reason. |
AD_DISABLED | 2001 | Ad disabled Ad serving has been disabled for this placement/application. |
PROFIG_NOT_SYNCED | 2002 | Profig not sync An internal SDK error has occurred. |
AD_EXPIRED | 2003 | Ad expired The loaded ad is expired. You must call the show method within 4 hours after the load . |
SDK_INIT_NOT_CALLED | 2004 | SDK init not called The Ogury.start() method has not been called before a call to the load or show methods. |
ANOTHER_AD_ALREADY_DISPLAYED | 2005 | Another ad already displayed Another ad is already displayed on the screen. |
SDK_INIT_FAILED | 2006 | SDK init failed An error occurred during the initialization of the SDK. |
ACTIVITY_IN_BACKGROUND | 2007 | Activity in background You tried to show an ad while the application was in background. Make sure to only call theshow method when the application is visible to the user. |
AD_NOT_AVAILABLE | 2008 | Ad not available The server returns no ads. |
AD_NOT_LOADED | 2009 | Ad not loaded The server returns an ad but something went wrong with the ad precaching. |
Last modified 10mo ago