MediaView integration guide

  1. Add the Yandex Mobile Ads SDK and AppMetrica SDK dependencies to the build.gradle file in your app's module:

    dependencies {
        ...
        implementation 'com.yandex.android:mobileads:2.150'
        implementation 'com.yandex.android:mobmetricalib:3.13.1'
    }
  2. Load the native ad.
    1. To get native advertising, use the NativeAdLoader object.
    2. Call the loadAd method for the NativeAdLoader object. This method takes Activity Context and NativeAdLoaderConfiguration as arguments.
      Note.

      In the NativeAdLoaderConfiguration object, enter blockId. You can also specify additional settings for loading ads.

    3. To monitor the process of native ad loading, be sure to pass NativeAdLoader.OnImageAdLoadListener as an argument of NativeAdLoader.
  3. Display the native ad.

    Restriction. Requirements for mediaView size when displaying video ads

    Minimum size of an instance of the MediaView class, which supports video playback: 300x160 or 160x300 dp (density-independent pixels).

    To support video playback in native ads, we recommend setting the width for mediaView to at least 300 dp. To calculate the appropriate mediaView height value, use the result returned by the getAspectRatio method. When displaying ads using templates, the correct height for mediaView will be calculated automatically based on the width to height ratio.

    1. For an easy way to display an ad, use the templates.

      final NativeAdLoaderConfiguration adLoaderConfiguration =
                      new NativeAdLoaderConfiguration.Builder("blockId", shouldLoadImagesAutomatically).build();
      final NativeAdLoader nativeAdLoader = new NativeAdLoader(context, adLoaderConfiguration);
      nativeAdLoader.setNativeAdLoadListener(new NativeAdLoader.OnImageAdLoadListener() {
          @Override
          public void onImageAdLoaded(@NonNull final NativeImageAd nativeImageAd) {
              bindNativeAd(nativeImageAd);
          }
      
          @Override
          public void onAppInstallAdLoaded(@NonNull final NativeAppInstallAd nativeAppInstallAd) {
          	bindNativeAd(nativeAppInstallAd);
          }
      
          @Override
          public void onContentAdLoaded(@NonNull final NativeContentAd nativeContentAd) {
          	bindNativeAd(nativeContentAd);
          }
      
          @Override
          public void onAdFailedToLoad(@NonNull final AdRequestError adRequestError) {
      		//handle adRequestError
          }
      });
      
      private void bindNativeAd(@NonNull final NativeGenericAd nativeAd) {
          mNativeBannerView.setAd(nativeAd);
          mNativeBannerView.setVisibility(View.VISIBLE);
      }
    2. You can also use the manual method to display native ads. This allows you to adapt native ads to your main content as closely as possible.

      private void bindNativeAd(@NonNull final NativeGenericAd nativeAd) {
          final NativeAdViewBinder nativeAdViewBinder = new NativeAdViewBinder.Builder(mNativeAdView)
                  .setAgeView((TextView) findViewById(R.id.age))
                  .setBodyView((TextView) findViewById(R.id.body))
                  .setCallToActionView((Button) findViewById(R.id.call_to_action))
                  .setDomainView((TextView) findViewById(R.id.domain))
                  .setFaviconView((ImageView) findViewById(R.id.favicon))
                  .setFeedbackView((Button) findViewById(R.id.feedback))
                  .setIconView((ImageView) findViewById(R.id.icon))
                  .setMediaView((MediaView) findViewById(R.id.media))
                  .setPriceView((TextView) findViewById(R.id.price))
                  .setRatingView((MyRatingView) findViewById(R.id.rating))
                  .setReviewCountView((TextView) findViewById(R.id.review_count))
                  .setSponsoredView((TextView) findViewById(R.id.sponsored))
                  .setTitleView((TextView) findViewById(R.id.title))
                  .setWarningView((TextView) findViewById(R.id.warning))
                  .build();
      
              configureMediaView();
          try {
              nativeAd.bindNativeAd(nativeAdViewBinder);
              mNativeAdView.setVisibility(View.VISIBLE);
          } catch (NativeAdException exception) {
              //handle exception
          }
      }
      
      private void configureMediaView(@NonNull final NativeGenericAd nativeAd) {
          final NativeAdMedia nativeAdMedia = nativeAd.getAdAssets().getMedia();
          if (nativeAdMedia != null) {
              //you can use the aspect ratio if you need it to determine the size of media view.
              final float aspectRatio = nativeAdMedia.getAspectRatio();
          }
      }
  4. For more information about native advertising, see Enabling native ads.