---
description: Integrate Infeed Banners
---

# Infeed Banner

Infeed banner placements are designed to serve ads in scrollable feeds (nevertheless, they can also be used in all other scenarios). This includes giving the app complete control over the lifecycle of a specific banner impression. The app can load an ad with a specific configuration and gets notified about the outcome of that specific load request (successful, failure). Hence, the usage of an infeed banner differs from using sticky banners.

### Create Placement

To create an instance of [InfeedBannerPlacement](https://android-sdk.aatkit.com/references/-a-a-t-kit/com.intentsoftware.addapptr/-infeed-banner-placement/index.html), use the following API:

{% tabs %}
{% tab title="Java" %}
```java
BannerConfiguration configuration = new BannerConfiguration();
InfeedBannerPlacement placement = AATKit.createInfeedBannerPlacement("<PLACEMENT_NAME>", configuration);
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
val configuration = BannerConfiguration()
val placement = AATKit.createInfeedBannerPlacement("<PLACEMENT_NAME>", configuration)
```
{% endtab %}
{% endtabs %}

#### **BannerConfiguration**

[BannerConfiguration](https://android-sdk.aatkit.com/references/-a-a-t-kit/com.intentsoftware.addapptr/-banner-configuration/index.html) can be configured with the number of workers and whether manual ad space counting is enabled.

* Manual ad space counting (Please refer to our article about [Ad Spaces](../../advanced/ad-space-and-fill-rate).)
  * `false` (default): infeed banner placement counts an Ad Space every time the app requests an ad from it. Shall only be used, if the app is presenting banners immediately (= not caching them for later use).
  * `true`: the app needs to notify the infeed banner placement about every Ad Space it creates (= placement doesn’t count Ad Spaces itself). This is especially useful if the app implements its own banner caching (e.g. in order to create a smooth user experience for the feed). In this case, the app should notify the placement about an Ad Space only if the feed cell intended for presenting a banner is reaching the visible area of the screen (regardless of whether an ad was available for it or not). If this sounds interesting to you, we strongly advise visiting our chapter about AATKit’s [Banner Cache](banner-cache).
  * To manually count an ad space, call the following `InfeedBannerPlacement` API:

{% tabs %}
{% tab title="Java" %}
```java
// call it when Infeed Ad is about to be displayed
placement.countAdSpace();
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
// call it when Infeed Ad is about to be displayed
placement.countAdSpace()
```
{% endtab %}
{% endtabs %}

* Number of workers
  * Default: 3
  * Defines, how many ads can be loaded in parallel. Higher numbers will result in faster ad delivery but also in more CPU and network traffic consumption

### Listen to Callbacks (Optional)

Through the use of [InfeedBannerPlacementListener](https://android-sdk.aatkit.com/references/-a-a-t-kit/com.intentsoftware.addapptr/-infeed-banner-placement-listener/index.html), you can listen to the different placement callbacks.

{% tabs %}
{% tab title="Java" %}
```java
placement.setListener(this);
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
placement.lisenet = this
```
{% endtab %}
{% endtabs %}

### Request Ad

The infeed banner placement uses the request completion listener ([BannerRequestCompletionListener](https://android-sdk.aatkit.com/references/-a-a-t-kit/com.intentsoftware.addapptr/-banner-request-completion-listener/index.html)) to notify about successful ad load or failure. To request a banner, use the [BannerRequest](https://android-sdk.aatkit.com/references/-a-a-t-kit/com.intentsoftware.addapptr/-banner-request/index.html) default initializer that takes an instance implementing [BannerRequestDelegate](https://android-sdk.aatkit.com/references/-a-a-t-kit/com.intentsoftware.addapptr/-banner-request-delegate/index.html) as a parameter.

#### **Configure AATBannerRequest**

The banner request can be configured with:

* A set of banner sizes of type [BannerSize](https://android-sdk.aatkit.com/references/-a-a-t-kit/com.intentsoftware.addapptr/-banner-size/index.html).
* Targeting Information.
* Content-targeting URL.

{% tabs %}
{% tab title="Java" %}
```java
BannerRequest request = new BannerRequest(this);
Map<String, List<String>> targeting = new HashMap<>();
targeting.put("key", Arrays.asList("value1", "value2"));
request.setTargetingInformation(targeting);
Set<BannerSize> allowedBannerSizes = new HashSet<>();
allowedBannerSizes.add(BannerSize.Banner320x53);
allowedBannerSizes.add(BannerSize.Banner300x250);
request.setBannerSizes(allowedBannerSizes);

request.setContentTargetingUrl("http://example.com/similar/content");
OR
request.setMultiContentTargetingUrls(Arrays.asList("URL1", "URL2"));
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
val request = BannerRequest(this)
val targeting = mapOf("key" to listOf("value1", "value2"))
request.targetingInformation = targeting
val allowedBannerSizes = setOf(BannerSize.Banner320x53, BannerSize.Banner300x250)
request.setBannerSizes(allowedBannerSizes)
request.contentTargetingUrl = "http://example.com/similar/content"
OR
request.multiContentTargetingUrls = listOf("URL1", "URL2")
```
{% endtab %}
{% endtabs %}

#### **Perform Request**

{% hint style="info" %}
You can request multiple `AATBannerRequest` in parallel (i.e. the next one, before the previous one was completed).
{% endhint %}

{% tabs %}
{% tab title="Java" %}
```java
BannerRequest request = new BannerRequest(this);
placement.requestAd(request, new BannerRequestCompletionListener() {
    @Override
    public void onRequestCompleted(@Nullable BannerPlacementLayout layout, @Nullable BannerRequestError error) {
        if (error != null) {
            //ad failed to load
            return;
        }
        // display the returned BannerPlacementLayout
    }
});
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
val request = BannerRequest(this)
placement.requestAd(request, object : BannerRequestCompletionListener {
    override fun onRequestCompleted(layout: BannerPlacementLayout?, error: BannerRequestError?) {
        if (error != null) {
            //ad failed to load
            return
        }
        // display the returned BannerPlacementLayout
    }
})
```
{% endtab %}
{% endtabs %}

### Cancel Request

There might be pending ad requests when the user navigates to a different activity. You should cancel any pending requests when that happens.

{% tabs %}
{% tab title="Java" %}
```java
placement.cancel(request);
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
placement.cancel(request)
```
{% endtab %}
{% endtabs %}

### Destroy old banner

When you no longer need a banner, you have to destroy it manually by removing it from your view and calling the "destroy" method:

{% tabs %}
{% tab title="Java" %}
```java
[remove loadedMultiSizeBanner from your layout]
oldBanner.destroy();
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
[remove loadedMultiSizeBanner from your layout]
oldBanner.destroy()
```
{% endtab %}
{% endtabs %}

### Ad Info <a href="#a-d-info" id="a-d-info"></a>

After loading a banner view, you can access the loaded ad information by accessing the `adInfo` property of the loaded banner view:

{% tabs %}
{% tab title="Java" %}
```java
AdInfo adInfo = loadedBannerView.getAdInfo();
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
val info = loadedBannerView.adInfo
```
{% endtab %}
{% endtabs %}

### Complete Code Example

{% tabs %}
{% tab title="Java" %}
```java
private InfeedBannerPlacement placement;

private void setUpInfeedBannerPlacement() {
    BannerConfiguration configuration = new BannerConfiguration();
    placement = AATKit.createInfeedBannerPlacement("<PLACEMENT_NAME>", configuration);

    // Set placement listener to listen to the callbacks
    placement.setListener(this);
}

private void requestInfeedBanner() {
    // Create the banner request instance
    BannerRequest request = new BannerRequest(this);
    // Configure request banner sizes
    Set<BannerSize> allowedBannerSizes = new HashSet<>();
    allowedBannerSizes.add(BannerSize.Banner320x53);
    allowedBannerSizes.add(BannerSize.Banner300x250);
    request.setBannerSizes(allowedBannerSizes);
    // Configure request targeting information
    Map<String, List<String>> targeting = new HashMap<>();
    targeting.put("key", Arrays.asList("value1", "value2"));
    request.setTargetingInformation(targeting);
    // Configure request targeting URL
    request.setContentTargetingUrl("http://example.com/similar/content");
    // Perform the ad request
    placement.requestAd(request, new BannerRequestCompletionListener() {
        @Override
        public void onRequestCompleted(@Nullable BannerPlacementLayout layout, @Nullable BannerRequestError error) {
            if (error != null) {
                //ad failed to load
                return;
            }
            // display the returned BannerPlacementLayout
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    // [IMPORTANT] Notify AATKit about activity lifecycle
    AATKit.onActivityResume(this);
}

@Override
protected void onPause() {
    // [IMPORTANT] Notify AATKit about activity lifecycle
    AATKit.onActivityPause(this);
    super.onPause();
}

// InfeedBannerPlacementListener:
@Override
public void onPauseForAd(@NonNull Placement placement) {
    // App is paused after banner got clicked
}

@Override
public void onResumeAfterAd(@NonNull Placement placement) {
    // Back to the app after clicking on the ad
}

// BannerRequestDelegate:
@Override
public boolean shouldUseTargeting(@NonNull BannerRequest request, @Nullable AdNetwork network) {
    return true;
}
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
private var placement: InfeedBannerPlacement? = null

private fun setUpInfeedBannerPlacement() {
    val configuration = BannerConfiguration()
    placement = createInfeedBannerPlacement("<PLACEMENT_NAME>", configuration)

    // Set placement listener to listen to the callbacks
    placement?.listener = this
}

private fun requestInfeedBanner() {
    // Create the banner request instance
    val request = BannerRequest(this)
    //  Configure request banner sizes
    val allowedBannerSizes = setOf(BannerSize.Banner320x53, BannerSize.Banner300x250)
    request.setBannerSizes(allowedBannerSizes)
    // Configure request targeting information
    val targeting = mapOf("key" to listOf("value1", "value2"))
    request.targetingInformation = targeting
    // Configure request targeting URL
    request.contentTargetingUrl = "http://example.com/similar/content"
        
    // Perform the ad request
    placement?.requestAd(request, object : BannerRequestCompletionListener {
        override fun onRequestCompleted(layout: BannerPlacementLayout?, error: BannerRequestError?) {
            if (error != null) {
                //ad failed to load
                return
            }
            // display the returned BannerPlacementLayout
        }
    })
}

protected override fun onResume() {
    super.onResume()
    // [IMPORTANT] Notify AATKit about activity lifecycle
    AATKit.onActivityResume(this)
}

protected override fun onPause() {
    // [IMPORTANT] Notify AATKit about activity lifecycle
    AATKit.onActivityPause(this)
    super.onPause()
}

// InfeedBannerPlacementListener:
override fun onPauseForAd(placement: Placement) {
    // App is paused after banner got clicked
}

override fun onResumeAfterAd(placement: Placement) {
    // Back to the app after clicking on the ad
}

// BannerRequestDelegate:
override fun shouldUseTargeting(request: BannerRequest, network: AdNetwork?): Boolean {
    return true
}
```
{% endtab %}
{% endtabs %}
