Adding Companion Ads

This guide is intended for publishers interested in adding companion ads to their Android IMA implementation.

Prerequisites

  • Android application with the IMA SDK integrated. See the BasicExample if you don't already have an app with the SDK integrated.
  • An ad tag configured to return a companion ad.
    • If you need a sample, check out our FAQ.

Helpful primers

If you still need to implement the IMA SDK in your app, check out our Get Started guide.

Adding companion ads to your app

Create a ViewGroup to display your companion

Before requesting a companion, you need to create a space for it in your layout. In your layout XML, add a ViewGroup element; in this example, add a LinearLayout. In a later step you'll pass a reference to this element to your AdDisplayContainer. If you're integrating into the BasicExample app, add this to fragment_video.xml.

fragment_video.xml
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:layout_gravity="center_horizontal"
  android:gravity="center"
  android:textAlignment="center"
  android:id="@+id/companionAdSlot" />

Create a CompanionAdSlot

The next step is to build a CompanionAdSlot object, which will then be added to an ArrayList<CompanionAdSlot>. AdDisplayContainer takes a list of companion ad slots so you can display multiple companion ads at once.

  • If you're not integrating into BasicExample, you'll need to first get a reference to the ViewGroup where the companion will be rendered:
    ViewGroup mCompanionViewGroup = (ViewGroup) rootView.findViewById(R.id.companionAdSlot);
  • If you are integrating into BasicExample, you'll need to pass a reference to the companion ViewGroup in your VideoPlayerController, like this:

    VideoPlayerController.java

    // Set up spots for companions.
    CompanionAdSlot companionAdSlot = mSdkFactory.createCompanionAdSlot();
    companionAdSlot.setContainer(mCompanionViewGroup);
    companionAdSlot.setSize(728, 90);
    ArrayList<CompanionAdSlot> companionAdSlots = new ArrayList<CompanionAdSlot>();
    companionAdSlots.add(companionAdSlot);
    mAdDisplayContainer.setCompanionSlots(companionAdSlots);

That's all there is to it! Your application is now displaying companion ads.

FAQ

I followed the guide, but I'm not seeing companion ads. What should I do?
First, check to make sure your tag really is returning companions. To do this, open the tag in a web browser and look for a CompanionAds tag. If you see that, check to make sure the size of the companion being returned is the same size as the dimensions you're passing into the CompanionAdSlot object.

Send feedback about...

IMA SDK for Android
Need help? Visit our support page.