# Banner Ads

## Banner Ads

**Banners** are rectangular ads that appear at the top or bottom of the screen when the user interacts with your app. The user can view banners but cannot dismiss them. The DT FairBid SDK automatically refreshes banners after 20 seconds.

{% hint style="info" %}
The DT FairBid SDK automatically refreshes banners. To avoid discrepancies between DT and third-party network reporting, *disable* any automatic or manual banner refresh settings on third-party network SDKs.
{% endhint %}

### Showing a Banner

Implement the code below to show a Banner:

{% tabs %}
{% tab title="Android" %}
{% code title="Kotlin" %}

```kotlin
val placementId = "12345"
Banner.show(placementId, activity)
```

{% endcode %}

{% code title="Java" %}

```java
String placementId = "12345";
Banner.show(placementId, activity);
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
extension MyViewController: FYBBannerDelegate {
  func bannerDidLoad(
    _ banner: FYBBannerAdView,
    impressionData _: FYBImpressionData
  ) {
    // set up the banner constraints
    myView.addSubview(banner)
    banner.center = CGPoint(x: myView.frame.width / 2, y: myView.center.y)
  }

  …
}
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
// in implementation of FYBBannerDelegate protocol

- (void)bannerDidLoad:(FYBBannerAdView *)banner
       impressionData:(FYBImpressionData *)impressionData {
  [self.myView addSubview:banner];
  banner.center = CGPointMake(self.myView.bounds.size.width / 2,
                              self.bounds.size.height / 2);
}
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
{% code title="C#" %}

```csharp
string placementId = "1234";
Banner.Show(placementId);
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Hiding the Banner

If you want to hide the banner temporarily, implement the code below:

{% tabs %}
{% tab title="Android" %}
{% code title="Kotlin" %}

```kotlin
//hides the banner for a specific placement
val placementId = "12345"
Banner.hide(placementId)
```

{% endcode %}

{% code title="Java" %}

```java
//hides the banner for a specific placement
String placementId = "12345";
Banner.hide(placementId);
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
let placementId = "1234" FYBBanner.hide(placementId)
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
NSString *placementId = @"1234"; [FYBBanner hide:placementId];
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
{% code title="C#" %}

```csharp
string placementId = "1234";
Banner.Hide(placementId);
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Destroying the Banner

Once you have decided that you no longer want to use the banner, you must destroy it.

To destroy the banner, implement the code below:

{% tabs %}
{% tab title="Android" %}
{% code title="Kotlin" %}

```kotlin
//destroys the banner for a specific placement
val placementId = "12345"
Banner.destroy(placementId)
```

{% endcode %}

{% code title="Java" %}

```java
//destroys the banner for a specific placement
String placementId = "12345";
Banner.destroy(placementId);
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
let placementId = "1234" FYBBanner.destroy(placementId)
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
NSString *placementId = @"1234"; [FYBBanner destroy:placementId];
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
{% code title="C#" %}

```csharp
string placementId = "1234";
Banner.Destroy(placementId);
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Banner Position

You can show your banner at the top or bottom of your screen. If you want to change this behavior, use the following code:

{% tabs %}
{% tab title="Android" %}

#### Top

{% code title="Kotlin" %}

```kotlin
val bannerOptions = BannerOptions().placeAtTheTop()
val placementId = "12345"
Banner.show(placementId, bannerOptions, activity)
```

{% endcode %}

{% code title="Java" %}

```java
BannerOptions bannerOptions = new BannerOptions().placeAtTheTop();
String placementId = "12345";
Banner.show(placementId, bannerOptions, activity);
```

{% endcode %}

#### Custom View

{% code title="Kotlin" %}

```kotlin
val bannerOptions = BannerOptions().placeInContainer(viewGroup)
val placementId = "12345"
Banner.show(placementId, bannerOptions, activity)
```

{% endcode %}

{% code title="Java" %}

```java
BannerOptions bannerOptions = new BannerOptions().placeInContainer(viewGroup);
String placementId = "12345";
Banner.show(placementId, bannerOptions, activity);)
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
This feature is not available for iOS.
{% endtab %}

{% tab title="Unity" %}
{% code title="C#" %}

```csharp
string placementId = "1234";
BannerOptions bannerOptions = new BannerOptions();
bannerOptions.DisplayAtTheTop();
Banner.Show(placementId, bannerOptions);
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Loading a Banner

{% hint style="warning" %}
Use this implementation only if DT FairBid is not your main mediation platform.
{% endhint %}

If you want to use DT FairBid with other mediations or demand sources, you can pre-load a banner to review its pricing and other details. This allows you to compare it against banners from other demand sources outside our platform before showing it.

{% tabs %}
{% tab title="Android" %}
To load a banner, create a `BannerView` object instance and then call the `load` method. Please be aware that this integration method disables banner refresh, and you must add your own refresh logic.

This is supported starting DT FairBid Android SDK 3.51.0.

{% code title="Kotlin" %}

```kotlin
val bannerContainer: FrameLayout
val placementId = "12345"

val bannerView = BannerView(requireContext(), placementId)
banner.bannerListener = object : BannerListener {
  //...
}
bannerView.load()

//Once the ad is loaded and ready, you can check the pre-impression data
val impressionData = bannerView.impressionData

// To show the banner, attach the bannerView to the Banner Container
bannerContainer.addView(bannerView)

//Once the banner is no longer needed, you can destroy it
bannerView.destroy()
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}

You are responsible for adding `FYBBannerAdView` instance to your view hierarchy. This `FYBBannerAdView` instance is available on `bannerDidLoad:impressionData: callback`. To pre-load a banner, implement the code below:

{% code title="Swift" %}

```swift
/* 1. Assign FYBBannerDelegate instance to handle banner events */
FYBBanner.delegate = self

/* 2. Define the banner placement ID
 Replace `placementId` with your actual banner placement ID */
let placementId = "<#banner placementId#>"

/* 3. Initialize a new FYBBannerOptions instance with a specified placement ID and size.
 FYBBannerSizeSmart automatically adjusts the banner size based on the device's screen width. */
let bannerOptions = FYBBannerOptions(placementId: placementId, size: .smart)

/* 4. Request to load a banner ad with the specified options.
 This method fetches the banner creative but does not show it. */
FYBBanner.request(with: bannerOptions)
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
#import <FairBidSDK/FairBid.h>
/// 1. Assign FYBBannerDelegate instance to handle banner events
FYBBanner.delegate = self;

/// 2. Define the banner placement ID
/// Replace banner placementId with your actual banner placement id
NSString *placementId = @"<#banner placementId#>";

/// 3. Initialize a new FYBBannerOptions instance with a specified placement ID
/// and size. FYBBannerSizeSmart automatically adjusts the banner size based on
/// the device's screen width.
FYBBannerOptions *bannerOptions =
    [[FYBBannerOptions alloc] initWithPlacementId:placementId
                                             size:FYBBannerSizeSmart];

/// 4. Request to load a banner ad with the specified options. This method
/// fetches the banner creative but does not show it.
[FYBBanner requestWithOptions:bannerOptions];
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
This feature is not available on Unity.
{% endtab %}
{% endtabs %}

### Adaptive Banners

By enabling the adaptive banner feature, you can receive the best banner size based on the ad width and screen size. **This feature is currently supported by Google AdMob, Google Bidding, and Google Ad Manager only**. This feature is disabled by default.

Networks that support adaptive banners will return ads with the best-fit height based on your banner size. Other networks will continue to deliver banners according to the specified ad size.

To use this feature, add the `adaptive` flag when defining your banner size using the code below:

{% tabs %}
{% tab title="Android" %}
{% code title="Kotlin" %}

```kotlin
val bannerOptions = BannerOptions().setAdaptive(true)
val placementId = "12345"
Banner.show(placementId, bannerOptions, activity)
```

{% endcode %}

{% code title="Java" %}

```java
BannerOptions bannerOptions = new BannerOptions().setAdaptive(true);
String placementId = "12345";
Banner.show(placementId, bannerOptions, activity);
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
let view: UIView = parent!.navigationController!.tabBarController!.view
let placementId = "12345"
let options = FYBBannerOptions(placementId: placementId, position: .bottom)
options.adaptive = true
FYBBanner.show(in: view, options: options)
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
NSString *placementId = @"12345";
FYBBannerOptions *options =
    [[FYBBannerOptions alloc] initWithPlacementId:placementId
                                         position:FYBBannerAdViewPositionTop];
options.adaptive = YES;
[FYBBanner showBannerInView:view options:options];
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
This feature is not available on Unity.
{% endtab %}
{% endtabs %}

### Adding Callbacks

The callback code below is required for the SDK to properly track the activity of your ad.

{% tabs %}
{% tab title="Android" %}
{% code title="Kotlin" %}

```kotlin
Banner.setBannerListener(object : BannerListener {
    override fun onError(placementId: String, error: BannerError) {
        // Called when an error arises when showing the banner from placement 'placementId'
    }

    override fun onLoad(placementId: String) {
        // Called when the banner from placement 'placementId' is successfully loaded
    }

    override fun onShow(placementId: String, impressionData: ImpressionData) {
        // Called when the banner from placement 'placementId' is shown
    }

    override fun onClick(placementId: String) {
        // Called when the banner from placement 'placementId' is clicked
    }

    override fun onRequestStart(placementId: String, requestId: String) {
        // Called when the banner from placement 'placementId' is going to be requested
        // 'requestId' identifies the request across the whole request/show flow
    }
})
```

{% endcode %}

{% code title="Java" %}

```java
Banner.setBannerListener(new BannerListener() {
    @Override
    public void onError(String placementId, BannerError error) {
        // Called when an error arises when showing the banner from placement 'placementId'
    }

    @Override
    public void onLoad(String placementId) {
        // Called when the banner from placement 'placementId' is successfully loaded
    }

    @Override
    public void onShow(String placementId, ImpressionData impressionData) {
        // Called when the banner from placement 'placementId' is shown
    }

    @Override
    public void onClick(String placementId) {
        // Called when the banner from placement 'placementId' is clicked
    }

    @Override
    public void onRequestStart(String placementId, String requestId) {
        // Called when the banner from placement 'placementId' is going to be requested
        // 'requestId' identifies the request across the whole request/show flow
    }
});
```

{% endcode %}
{% endtab %}

{% tab title="iOS" %}
{% code title="Swift" %}

```swift
class MyBannerDelegate: NSObject, FYBBannerDelegate {
    func bannerDidLoad(_ banner: FYBBannerAdView, impressionData: FYBImpressionData) {}
    func bannerDidFail(toLoad placementId: String,     withError error: Error) {}
    func bannerDidShow(_ banner: FYBBannerAdView, impressionData: FYBImpressionData) {}
    func bannerDidClick(_ banner: FYBBannerAdView){}
    func bannerWillPresentModalView(_ banner: FYBBannerAdView) {}
    func bannerDidDismissModalView(_ banner: FYBBannerAdView) {}
    func bannerWillLeaveApplication(_ banner: FYBBannerAdView) {}
    func banner(_ banner: FYBBannerAdView, didResizeToFrame frame: CGRect) {}
    func bannerWillRequest(_ placementId: String, withRequestId requestId: String) {} }
```

{% endcode %}

{% code title="Objective-C" %}

```objective-c
#import FairBidSDK/FairBid.h
@interface MyBannerDelegate : NSObject
@end @implementation MyBannerDelegate
- (void)bannerDidLoad:(FYBBannerAdView *)banner impressionData:(FYBImpressionData *)impressionData {
// Called when an ad is loaded
}

- (void)bannerDidFailToLoad:(NSString *)placementId withError:(NSError *)error {
// Called when an error arises when loading an ad
}

- (void)bannerDidShow:(FYBBannerAdView *)banner impressionData:(FYBImpressionData *)impressionData {
// Called when banner shows up
}

- (void)bannerDidClick:(FYBBannerAdView *)banner {
// Called when banner is clicked
}

- (void)bannerWillPresentModalView:(FYBBannerAdView *)banner {
// Called when banner presents modal view
}

- (void)bannerDidDismissModalView:(FYBBannerAdView *)banner {
// Called when banner hides presented modal view
}

- (void)bannerWillLeaveApplication:(FYBBannerAdView *)banner {
// Called after banner redirects to other application
}

- (void)banner:(FYBBannerAdView *)banner didResizeToFrame:(CGRect)frame {
// Called after banner changes its size to desired frame
}

- (void)bannerWillRequest:(NSString *)placementId withRequestId:(NSString *)requestId  {
// Called when a banner is going to be requested.
}

@end
```

{% endcode %}
{% endtab %}

{% tab title="Unity" %}
{% code title="C#" %}

```csharp
public class MyBannerListener : BannerListener
{
    public void OnError(string placementId, string error)
    {
        // Called when an error from placement 'placementId' arises when loading an ad
    }

    public void OnLoad(string placementId)
    {
        // Called when an ad from placement 'placementId' is loaded
    }

    public void OnShow(string placementId, ImpressionData impressionData)
    {
        // Called when banner from placement 'placementId' shows up
    }
    public void OnClick(string placementId)
    {
        // Called when banner from placement 'placementId' is clicked
    }

    public void OnRequestStart(string placementId)
    {
        // Called when a banner from placement 'placementId' is going to be requested
    }
}

Banner.SetBannerListener(new MyBannerListener());
```

{% endcode %}
{% endtab %}
{% endtabs %}
