Banner Ads

Set up a Banner Ad

  1. From the left navigation, select Inventory Inventory Settings.
  2. Search for the app or website you would like to create a placement for and click + Add a placement.
  3. Click Select Ad Unit and select Banner.
  4. Click Add a placement to start setting up your banner settings. After you create the banner placement, you can see the placement ID.

Add the Banner Ad to your App

Creating a Banner Ad

Option 1: Adding a Banner in XML layout resource files

You can add a banner ad in your layout resource file in XML and reference it in your code as you would in a regular view.

The following snippet shows how to add a banner ad in XML. Note the InMobi ads namespace that you need to add to the top-level layout element to add the banner.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.inmobi.ads" 
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
   ...  
   > 
  
    <com.inmobi.ads.InMobiBanner 
        android:layout_width="320dp" 
        android:layout_height="50dp" 
        android:id="@+id/banner" 
        ads:placementId="plid-1431977751489005" 
        ads:refreshInterval="60" 
    /> 
</LinearLayout>

For banner XML integration, publishers using Android SDK 720 and onwards needs to append “plid-” to placementId TAG as following:

ads:placementId="plid-1431977751489005"

Now, you can reference the banner in your code:

Java

InMobiBanner bannerAd = (InMobiBanner)findViewById(R.id.banner);

Kotlin

val bannerAd:InMobiBanner = findViewById(R.id.banner)

Once you have the banner instance, you can request for ads to be loaded as shown in the following sections.

Before you do that, call the InMobiSdk.init(Context, String) method in your main activity or the activity where you are displaying banner ad to initialize the InMobi SDK.

Option 2: Adding a Banner in code

To add a banner ad in Android code, create an instance of InMobiBanner:

Java

InMobiBanner bannerAd = new InMobiBanner(BannerAdsActivity.this, 1468078426600L); 

Kotlin

val bannerAd = InMobiBanner(this@BannerAdsActivity, 1468078426600L)

Note

  • The InMobiBanner class is not thread-safe. A banner instance must be created on the UI thread.
  • Similarly, all methods on this instance must be called on the UI thread. Not doing so will lead to unpredictable behavior and may affect your ability to monetize with InMobi.
  • Creating an InMobiBanner object before SDK initialization will throw an exception.

Once you have created a banner ad, you can add it to the view hierarchy:

Java

RelativeLayout adContainer = (RelativeLayout) findViewById(R.id.ad_container);
RelativeLayout.LayoutParams bannerLp = new RelativeLayout.LayoutParams( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > );
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
adContainer.addView(bannerAd, bannerLayoutParams);

Kotlin

val adContainer:RelativeLayout = findViewById(R.id.ad_container) 
val bannerLp:RelativeLayout.LayoutParams = RelativeLayout.LayoutParams( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > ) 
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) 
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL) 
adContainer.addView(bannerAd, bannerLayoutParams)

Note

  • You should specify the banner view dimensions in pixel units when setting the layout parameters for the banner ad.
  • Also, it is a programming error to supply WRAP_CONTENT as the layout parameters for a banner ad. This constraint applies whether you create a banner in your XML layout resource file or in code.
  • If you are building for both phones and tablets, you can supply MATCH_PARENT as the layout parameters for the banner. The InMobi SDK will correctly compute the banner view dimensions and use them to fetch an ad from the InMobi Network.

Loading a Banner Ad

Loading a banner ad requires you to add the banner ad view to your view hierarchy before requesting for an ad on the InMobiBanner instance. Adding the view to the view hierarchy enables the InMobi SDK to correctly compute the banner view dimensions and to request for the best ad matching those dimensions from the network.

Once you have added InMobiBanner to the view hierarchy, you can call the load() method on the instance you just created to request for an ad.

Optimization

Setting up Auto-refresh for your Banner Ad

The banner ads refresh automatically after the first time you requested for an ad by calling load() on an InMobiBanner instance. You can set up auto-refresh either while creating a banner in XML layout resource file, or later in code, by calling the setRefreshInterval(int) method on a banner ad.

The sample below shows how to do this in XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.inmobi.ads" 
    xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent" 
    android:layout_height="match_parent"  
   ...
   >  
     
    <com.inmobi.ads.InMobiBanner 
        android:layout_width="320dp" 
        android:layout_height="50dp" 
        android:id="@+id/banner" 
        ads:placementId="plid-1431977751489005" 
ads:refreshInterval="60" 
    /> 
</LinearLayout>

Animating Banner Ad Refreshes

You can choose an animation style that takes effect when a banner is refreshed. You can also choose to turn OFF any animation for banner refreshes.

To do so, you can call the setAnimationType(InMobiBanner.AnimationType) method after obtaining a banner instance.

Note

The default animation is set to AnimationType.ROTATE_HORIZONTAL_AXIS. If you want to turn this off, call the setAnimationType method with the AnimationType.ANIMATION_OFF parameter.

Listening for Banner Ad Lifecycle Events

If you want to listen for key events in the banner lifecycle, you should implement the BannerAdListener interface. You can then set this listener using the setListener(BannerAdListener) method on the InMobiBanner instance. A brief description of each event is as follows.

Java

/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/
public abstract class BannerAdEventListener {
    /**  
     * Called to notify that an ad was received successfully but is not ready to be displayed yet.  
     *  
     * @param ad   Represents the ad which was loaded or preloaded  
     * @param info Represents the ad meta information  
     */ 
    public void onAdFetchSuccessful(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {} 
    /**  
     * Called to notify that an ad was successfully loaded.  
     * @param ad Represents the {@link InMobiBanner} ad which was loaded  
     * @param info Represents the ad meta information  
     */ 
    public void onAdLoadSucceeded(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {} 
    /**  
     * Called to notify that a request to load an ad failed.  
     * @param ad Represents the {@link InMobiBanner} ad which failed to load  
     * @param status Represents the {@link InMobiAdRequestStatus} status containing error reason
     */ 
    public void onAdLoadFailed(@NonNull InMobiBanner ad, @NonNull InMobiAdRequestStatus status) {} 
    /**  
     * Called to notify that the user interacted with the ad.  
     * @param ad Represents the {@link InMobiBanner} ad on which user clicked  
     * @param params Represents the click parameters  
     */ 
    public void onAdClicked(@NonNull InMobiBanner ad, Map < Object, Object > params) {} 
    /**  
     * Called to notify that the banner ad was displayed  
     * @param ad Represents the {@link InMobiBanner} ad which was displayed  
     */ 
    public void onAdDisplayed(@NonNull InMobiBanner ad) {} 
    /**  
     * Called to notify that the User is about to return to the application after closing the ad.  
     * @param ad Represents the {@link InMobiBanner} ad which was closed  
     */ 
    public void onAdDismissed(@NonNull InMobiBanner ad) {} 
    /**  
     * Called to notify that the user is about to leave the application as a result of interacting with the ad.  
     * @param ad Represents the {@link InMobiBanner} ad  
     */ 
    public void onUserLeftApplication(@NonNull InMobiBanner ad) {} 
    /**  
     * Called to notify that a reward was unlocked.  
     * @param ad Represents the {@link InMobiBanner} ad for which rewards was unlocked  
     * @param rewards Represents the rewards unlocked  
     */ 
    public void onRewardsUnlocked(@NonNull InMobiBanner ad, Map < Object, Object > rewards) {} 
    /** 
     * Called to notify that inmobi has logged an impression for the ad 
     * @param ad     Represents the ad which was impressed 
     */ 
    public void onAdImpression(@NonNull InMobiBanner ad) { 
    } 
}

Kotlin

/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/
abstract class BannerAdEventListener : AdEventListener<inmobibanner>() {
/**
    * Called to notify that an ad preload has failed.  
    *  
    ***Note** This notification is given only when you use `preload()` in  
    * [InMobiBanner.preloadManager]  
    *  
    * @param ad     Represents the [InMobiBanner] ad which was preloaded  
    * @param status Represents the [InMobiAdRequestStatus] status containing error reason 
    */  
open fun onAdFetchFailed(ad: InMobiBanner, status: InMobiAdRequestStatus) {}
/**
    * Called to notify that the banner ad has expanded  
    *  
    * @param ad Represents the [InMobiBanner] ad which was expanded  
    */  
open fun onAdDisplayed(ad: InMobiBanner) {}
/**
    * Called to notify that the User is about to return to the application after closing the ad.  
    *  
    * @param ad Represents the [InMobiBanner] ad which was closed  
    */  
open fun onAdDismissed(ad: InMobiBanner) {}
/**
    * Called to notify that the user is about to leave the application as a result of interacting with the ad.  
    *  
    * @param ad Represents the [InMobiBanner] ad  
    */  
open fun onUserLeftApplication(ad: InMobiBanner) {}
/**
    * Called to notify that a reward was unlocked.  
    *  
    * @param ad      Represents the [InMobiBanner] ad for which rewards was unlocked  
    * @param rewards Represents the rewards unlocked  
    */  
open fun onRewardsUnlocked(ad: InMobiBanner, rewards: Map<any, any="">) {}
    /** 
     * Called to notify that inmobi has logged an impression for the ad 
     * @param ad     Represents the ad which was impressed 
     */ 
open fun onAdImpression(ad: InMobiBanner) {}
}

You can check the code samples for banner ad integrations on GitHub here.

Deprecating Banner

If you’re done with the Banner object, then simply call deprecate on the same to release the resources. This will do the following:

  1. Remove the ad-view from the view hierarchy.
  2. Cancel any ongoing refresh.
  3. De-reference the listeners.
  4. Other clean-ups.

Java

bannerAd.destroy(); // bannerAd is an example instance.

Kotlin

bannerAd.destroy() // bannerAd is an example instance.

On This Page

Last Updated on: 01 Aug, 2022