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:
InMobiBanner bannerAd = (InMobiBanner)findViewById(R.id.banner);
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.
To add a banner ad in Android code, create an instance of InMobiBanner
:
InMobiBanner bannerAd = new InMobiBanner(BannerAdsActivity.this, 1468078426600L);
val bannerAd = InMobiBanner(this@BannerAdsActivity, 1468078426600L)
InMobiBanner
class is not thread-safe. A banner instance must be created on the UI thread.Once you have created a banner ad, you can add it to the view hierarchy:
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);
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)
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.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 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.
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>
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.
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.
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.
/**
* 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) {
}
}
/**
* 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.
If you’re done with the Banner object, then simply call deprecate on the same to release the resources. This will do the following:
bannerAd.destroy(); // bannerAd is an example instance.
bannerAd.destroy() // bannerAd is an example instance.
By installing this SDK update, you agree that your Children Privacy Compliance setting remains accurate or that you will update that setting, whenever there is a change in your app's audience. You may update the app's Children Privacy Compliance settings at https://publisher.inmobi.com/my-inventory/app-and-placements.