Advertisement
solodroid

loadNativeAd

Oct 6th, 2020
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1.     private void loadNativeAd() {
  2.         if (AdsConfig.NATIVE_AD_ON_NEWS_DETAIL) {
  3.             TemplateView native_template = findViewById(R.id.my_template);
  4.             MediaView mediaView = findViewById(R.id.media_view);
  5.             AdLoader adLoader = new AdLoader.Builder(this, getResources().getString(R.string.admob_native_unit_id))
  6.                     .forUnifiedNativeAd(unifiedNativeAd -> {
  7.                         if (sharedPref.getIsDarkTheme()) {
  8.                             ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.colorBackgroundDark));
  9.                             NativeTemplateStyle styles = new NativeTemplateStyle.Builder().withMainBackgroundColor(colorDrawable).build();
  10.                             native_template.setStyles(styles);
  11.                         } else {
  12.                             ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.colorBackgroundLight));
  13.                             NativeTemplateStyle styles = new NativeTemplateStyle.Builder().withMainBackgroundColor(colorDrawable).build();
  14.                             native_template.setStyles(styles);
  15.                         }
  16.                         mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
  17.                         native_template.setNativeAd(unifiedNativeAd);
  18.                         new Handler().postDelayed(() -> native_template.setVisibility(View.VISIBLE), 1500);
  19.                     })
  20.                     .withAdListener(new AdListener() {
  21.                         @Override
  22.                         public void onAdFailedToLoad(int errorCode) {
  23.                             native_template.setVisibility(View.GONE);
  24.                         }
  25.                     })
  26.                     .build();
  27.             adLoader.loadAd(Tools.getAdRequest(this));
  28.         }
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement