Get Started

The IMA Android SDK v3 enables developers to create Android apps that request and track VAST ads. For a detailed list of the video ad features supported by each of the IMA SDKs, see Video features and SDK versions.

This guide shows you how to integrate the Interactive Media Ads SDK into a sample video player app. Download the sample video player app (without any IMA integration) from GitHub, and at the end of the exercise the app plays a video ad before playing the app's content video. This guide should take about 30 minutes to complete.


Before you begin, you'll need the following:

If you'd rather follow along with a finished IMA SDK integration, download or view it on GitHub.

Downloading and running the sample video player app

The sample video player app provides a working simple video player app for you to use as a starting point for integrating the IMA Android SDK.

  1. Download the Sample Video Player app and unzip it.
  2. Start Android Studio and select Open an existing Android Studio project, or if Android Studio is already running, select File > New > Import Project. Then, choose SampleVideoPlayer/build.gradle.
  3. Run a Gradle sync by selecting Tools > Android > Sync Project with Gradle Files.
  4. Ensure that the player app compiles and runs on a physical Android device or an Android Virtual Device using Run > Run 'app'. It's normal for the content video to take a few moments to load before playing.

Examining the sample video player

The sample video player doesn't contain any IMA SDK integration code yet. The sample app consists of three major parts:

  • samplevideoplayer/ - A video implementation extending VideoView that has an interface for notifying listeners when the video finishes playing.
  • res/layout/fragment_video.xml - App layout XML containing SampleVideoPlayer and defining a play button.
  • videoplayerapp/ - This activity defines the method orientVideoDescriptionFragment to show or hide the VideoDescriptionFragment when appropriate (for example, to hide the description fragment to allow the video to go fullscreen). This file also defines VideoFragment, which sets the VideoPlayer content URL and wires the play button click event.

Adding the IMA Android SDK to the player app

Include Google Play services (preferably the latest version, but at least version 4.0) in your application following these instructions. You'll also include a reference to the IMA SDK. For Android Studio the integration involves the following:

  1. Add the following to your application-level build.gradle file, located at app/build.gradle:

    dependencies {
        compile ''
        compile ''
        compile ''
  2. Add the following <meta-data> code to your to AndroidManifest.xml inside the <application> tag:

                android:value="@integer/google_play_services_version" />


Your app should compile and run, but you haven't yet added UI elements or code to request and display ads. When you initialize and manipulate UI objects, you must do so on the main thread if you're using multiple threads. In Android, UI objects are not thread-safe. See Threads for more information.

Your first ads request

  1. Add ad tag
  2. Add an entry in strings.xml to store the VAST ad tag URL.


    <?xml version="1.0" encoding="utf-8"?>
        <string name="app_name">IMA Sample Video Player<string>
        <string name="action_settings">Settings<string>
        <string name="video_description">Android Promotional Video<string>
        <string name="content_url">…<string>
        <string name="ad_tag_url">

  3. Create an AdsLoader
  4. To request ads, first create an AdsLoader from the ImaSdkFactory. Then add both an AdsLoadedListener to handle ads being loaded, and an AdErrorListener to respond to loading errors. Add the following to VideoFragment.onActivityCreated():

    public void onActivityCreated(Bundle bundle) {
        // Create an AdsLoader.
        mSdkFactory = ImaSdkFactory.getInstance();
        mAdsLoader = mSdkFactory.createAdsLoader(this.getContext());
        // Add listeners for when ads are loaded and for errors.
        mAdsLoader.addAdsLoadedListener(new AdsLoadedListener() {
            public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
                // Ads were successfully loaded, so get the AdsManager instance. AdsManager has
                // events for ad playback and errors.
                mAdsManager = adsManagerLoadedEvent.getAdsManager();
                // Attach event and error event listeners.

  5. Add completion and click listeners
  6. The video player fires a VideoCompletedListener when the content video finishes. Add a listener to notify the AdsLoader when the content finishes. Then add an OnClickListener to request ads when the play button is clicked:

    public void onActivityCreated(Bundle bundle) {
        // Create an AdsLoader.
        // Add listener for when the content video finishes.
        mVideoPlayer.addVideoCompletedListener(new OnVideoCompletedListener() {
            public void onVideoCompleted() {
                // Handle completed event for playing post-rolls.
                if (mAdsLoader != null) {
        // When Play is clicked, request ads and hide the button.
        mPlayButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

  7. Request ads
  8. Implement the requestAds() method to create the AdsRequest. As part of the request, provide a ContentProgressProvider to inform the SDK about the content video's progress, which is used to determine when to play mid-rolls. End the function by calling AdsLoader.requestAds() to send the request.

    private void requestAds(String adTagUrl) {
        AdDisplayContainer adDisplayContainer = mSdkFactory.createAdDisplayContainer();
        // Create the ads request.
        AdsRequest request = mSdkFactory.createAdsRequest();
        request.setContentProgressProvider(new ContentProgressProvider() {
            public VideoProgressUpdate getContentProgress() {
                if (mIsAdDisplayed || mVideoPlayer == null || mVideoPlayer.getDuration() <= 0) {
                    return VideoProgressUpdate.VIDEO_TIME_NOT_READY;
                return new VideoProgressUpdate(mVideoPlayer.getCurrentPosition(),
        // Request the ad. After the ad is loaded, onAdsManagerLoaded() will be called.

  9. Handling ad events
  10. The app must handle ad events sent from the SDK to notify it about major ad events. Implement the onAdEvent() method to handle events sent from AdsManager. Of particular importance is to call AdsManager.start() to start playing ads, and to pause and resume the content video when ads start and end.

    public void onAdEvent(AdEvent adEvent) {
        Log.i(LOGTAG, "Event: " + adEvent.getType());
        // These are the suggested event types to handle. For full list of all ad event
        // types, see the documentation for AdEvent.AdEventType.
        switch (adEvent.getType()) {
            case LOADED:
                // AdEventType.LOADED will be fired when ads are ready to be played.
                // AdsManager.start() begins ad playback. This method is ignored for VMAP or
                // ad rules playlists, as the SDK will automatically start executing the
                // playlist.
                // AdEventType.CONTENT_PAUSE_REQUESTED is fired immediately before a video
                // ad is played.
                mIsAdDisplayed = true;
                // AdEventType.CONTENT_RESUME_REQUESTED is fired when the ad is completed
                // and you should start playing your content.
                mIsAdDisplayed = false;
            case ALL_ADS_COMPLETED:
                if (mAdsManager != null) {
                    mAdsManager = null;

  11. Errors, pause and resume
  12. The app also needs to handle errors thrown by the SDK. Implement onAdError() to log errors and to resume content video playback if the ads can't play. Change the onPause() and onResume() methods to call AdsManager if playing an ad.

    public void onAdError(AdErrorEvent adErrorEvent) {
        Log.e(LOGTAG, "Ad Error: " + adErrorEvent.getError().getMessage());;
    public void onResume() {
        if (mAdsManager != null && mIsAdDisplayed) {
        } else {
    public void onPause() {
        if (mAdsManager != null && mIsAdDisplayed) {
        } else {

Congrats! You're now requesting and displaying video ads in your Android app. To fine tune your implementation, see Ad Rules and the API documentation.


If you're experiencing issues playing a video ad, try downloading the completed BasicExample below and change the ad_tag_url defined in strings.xml to the ad tag URL you're testing with. If it works properly in the BasicExample then there's likely an issue with your app's IMA integration code. Review this guide and API docs to detect any discrepancies.

Download View on GitHub

Still having issues? Drop us a line in the IMA SDK forum.

Send feedback about...

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