Inline Placements

Inline ads, commonly referred to as “banners” are ad units that appear along with with other app content. Inline Ads are often found at the top or bottom of a mobile device’s screen while reading articles and searching through content. We recommend using inline ads on both smartphones and tablets in cases where the user is reading and/or interacting with content on the same screen for a period of time.

Integrating Inline Ad Placements

To add an inline ad to an application, import the required classes, instantiate an inline ad, load the ad, and attach it to the app’s view hierarchy.

1. Import the required classes

Import the following into the Android activity or iOS view controller that will contain the ad.

// Required Yahoo Mobile SDK imports for inline ads
import com.yahoo.ads.ErrorInfo;
import com.yahoo.ads.inlineplacement.AdSize;
import com.yahoo.ads.inlineplacement.InlineAdView;
import com.yahoo.ads.inlineplacement.InlinePlacementConfig;
// Required Yahoo Mobile SDK imports for inline ads
import com.yahoo.ads.ErrorInfo
import com.yahoo.ads.inlineplacement.AdSize
import com.yahoo.ads.inlineplacement.InlineAdView
import com.yahoo.ads.inlineplacement.InlinePlacementConfig
//Required Yahoo Mobile SDK imports for inline ads
#import <YahooAds/YahooAds.h>
//Required Yahoo Mobile SDK imports for inline ads
import YahooAds

2. Create an inline ad placement configuration

An instance of the inline placement config is required to load an ad. The config is created once, typically when the app’s activity or view controller is created.

// Create placement config required to load the ad with example dimensions
int width = 320, height = 50;
AdSize adSize = new AdSize(width, height);
List<AdSize> adSizes = new ArrayList<>();
adSizes.add(adSize);
InlinePlacementConfig inlinePlacementConfig = new InlinePlacementConfig(PLACEMENT_ID, null, adSizes);
// Create placement config required to load the ad with example dimensions
val width = 320
val height = 50
val adSize = AdSize(width, height)
val adSizes = ArrayList<AdSize>()
adSizes.add(adSize)
val placementConfig = InlinePlacementConfig(PLACEMENT_ID, null, adSizes)
// Create placement config required to load the ad with example dimensions
YASInlineAdSize *adSize = [[YASInlineAdSize alloc] initWithWidth:320 height:50];
YASInlinePlacementConfig *inlineBannerConfig = [[YASInlinePlacementConfig alloc] initWithPlacementId:placementID
                                                                                     requestMetadata:nil
                                                                                             adSizes:@[adSize]];
// Create placement config required to load the ad with example dimensions
let adSize = YASInlineAdSize(width: 320, height: 50)
let inlineBannerConfig = YASInlinePlacementConfig(placementId: placementId,
                                                  requestMetadata: nil,
                                                  adSizes: [adSize])

3. Load and show an ad

Create an instance of an InlineAdView, then load, and show the ad.

// Instantiate InlineAdView and Listen for Inline Ad callbacks
InlineAdView inlineAdView = new InlineAdView(context, PLACEMENT_ID, inlineAdListener);
runOnUiThread(() -> {
    // Load the ad
    inlineAdView.load(inlinePlacementConfig);
});
// Instantiate InlineAdView and Listen for Inline Ad callbacks
val inlineAdView = InlineAdView(context, PLACEMENT_ID, inlineAdListener)
runOnUiThread {
    // Load the ad
    inlineAdView.load(inlinePlacementConfig)
}
// Instantiate a YASInlineAdView
YASInlineAdView *inlineAdView = [[YASInlineAdView alloc] initWithPlacementId:placementID];
// Retain the ad object
self.inlineAdView = inlineAdView;
// Listen for inline ad callbacks
self.inlineAdView.delegate = self;
// Load the ad
[self.inlineAdView loadWithPlacementConfig:inlineBannerConfig];
// Instantiate a YASInlineAdView
guard let inlineAdView = YASInlineAdView(placementId: placementID) else { return }
// Retain the ad object
self.inlineAdView = inlineAdView
// Listen for inline ad callbacks
self.inlineAdView.delegate = self
// Load the ad
self.inlineAdView.load(with: inlineBannerConfig)

Inline ad callbacks

The Inline ad callbacks communicate events related to the life of the ad, such as Ad Loaded, Ad Load Failed, Clicked, Ad Left Application, etc. These are implemented as an interface (Android) and delegates (iOS). Please refer to the API documentation for Android and iOS for a complete list of available callbacks.

// InlineAdListener
@Override
void onLoaded(final InlineAdView inlineAdView) {
    // Show the ad
    runOnUiThread(() -> {
        adContainer.addView(inlineAdView);
    }
});

@Override
void onLoadFailed(final InlineAdView inlineAdView, final ErrorInfo errorInfo) {
    // Handle load error
    Log.e(TAG, errorInfo.toString());
});

@Override
public void onClicked(final InlineAdView inlineAdView) {
    Log.i(TAG, "Inline ad clicked");
}
// etc...
// InlineAdListener
override fun onLoaded(inlineAdView: InlineAdView) {
    // Show the ad
    runOnUiThread {
        adContainer.addView(inlineAdView)
    }
}

override fun onLoadFailed(inlineAdView: InlineAdView, errorInfo: ErrorInfo) {
    // Handle load error
    Log.e(TAG, errorInfo.toString())
}

override fun onClicked(inlineAdView: InlineAdView) {
    Log.i(TAG, "Inline ad clicked")
}
// etc...
// Inline ad callbacks
- (void)inlineAdDidLoad:(YASInlineAdView *)inlineAd
{
    // Show the ad
    dispatch_async(dispatch_get_main_queue(), ^{
        inlineAd.frame = self.inlineBannerContainer.bounds;
        [self.inlineBannerContainer addSubview:inlineAd];
    });
}

- (void)inlineAdLoadDidFail:(YASInlineAdView *)inlineAd withError:(YASErrorInfo *)errorInfo
{
    // Handle load error
    NSLog("Inline ad load did fail: %@", errorInfo);
}

- (void)inlineAdClicked:(YASInlineAdView *)inlineAd
{
    NSLog(@"Inline ad clicked");
}
// etc...
// Inline ad callbacks
func inlineAdDidLoad(_ inlineAd: YASInlineAdView) {
      // Show the ad
    DispatchQueue.main.async {
        // Show the ad
        inlineAd.frame = self.inlineBannerContainer.bounds
        self.inlineBannerContainer.addSubview(inlineAd)
    }
}

func inlineAdLoadDidFail(_ inlineAd: YASInlineAdView, withError errorInfo: YASErrorInfo) {
    // Handle load error
    print("Inline ad load did fail: \(errorInfo)")
}

func inlineAdClicked(_ inlineAd: YASInlineAdView) {
    print("Inline ad clicked")
}
// etc...

4. Android Only - Destroy the ad

When the ad instance is no longer needed or when the activity is finished, the instance must be destroyed to free up resources.

// Destroy the ad
inlineAdView.destroy()
// Destroy the ad
inlineAdView?.destroy()

Next Steps

  • If you were able to successfully load an ad, congratulations on a successful integration!
  • If you would like a complete example of implementing an Inline Ad within an app, please review the sample application.
  • If you are still having trouble, please contact your account manager.