Guest User

Untitled

a guest
Feb 17th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. NativeAdsManager listNativeAdsManager;
  2.  
  3. @Override
  4. protected void onStart() {
  5. super.onStart();
  6.  
  7. // Initialize the Audience Network SDK
  8. AudienceNetworkAds.initialize(this);
  9.  
  10. ... // interstitialAdFromFacebook works fine
  11.  
  12.  
  13. // Native Facebook ads
  14. listNativeAdsManager = new NativeAdsManager(this,fb_native,10);
  15. listNativeAdsManager.loadAds(NativeAd.MediaCacheFlag.ALL);
  16. }
  17.  
  18.  
  19. NativeAd loadAndShowNativeAd(){
  20. //calling this method from adapter Class.
  21.  
  22. try {
  23. return listNativeAdsManager.nextNativeAd();
  24. } catch (Exception E){
  25. return null;
  26. }
  27. }
  28.  
  29. void reloadNativeAds(){
  30. //this is also called from adapter class.
  31. try {
  32. listNativeAdsManager.loadAds(NativeAd.MediaCacheFlag.ALL);
  33.  
  34. } catch (Exception E){
  35.  
  36. }
  37. }
  38.  
  39. @Override
  40. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  41. ...
  42. // loading ad on every 7th recyclerview Item
  43. if (position %7 == 0 && position !=0){
  44. myHolder.ad_unit.setVisibility(View.VISIBLE);
  45.  
  46. try{
  47.  
  48.  
  49. NativeAd nativeAd = ((MainActivity)mContext).loadAndShowNativeAd();
  50.  
  51. if (nativeAd != null && nativeAd.isAdLoaded() && !nativeAd.isAdInvalidated()) {
  52. //show ad
  53. nativeAd.unregisterView();
  54.  
  55. // Add the Ad view into the ad container.
  56. NativeAdLayout nativeAdLayout = myHolder.ad_unit;
  57. LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  58. // Inflate the Ad view. The layout referenced should be the one you created in the last step.
  59. LinearLayout adView = (LinearLayout) inflater.inflate(R.layout.native_ad_layout, nativeAdLayout, false);
  60. nativeAdLayout.addView(adView);
  61.  
  62. // Add the AdOptionsView
  63.  
  64. LinearLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container);
  65. AdOptionsView adOptionsView = new AdOptionsView(((MainActivity)mContext), nativeAd, nativeAdLayout);
  66. adChoicesContainer.removeAllViews();
  67. adChoicesContainer.addView(adOptionsView, 0);
  68.  
  69. // Create native UI using the ad metadata.
  70. AdIconView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
  71. TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
  72. MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
  73. TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
  74. TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
  75. TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
  76. Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
  77.  
  78. // Set the Text.
  79. nativeAdTitle.setText(nativeAd.getAdvertiserName());
  80. nativeAdBody.setText(nativeAd.getAdBodyText());
  81. nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
  82. nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
  83. nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
  84. sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
  85.  
  86. // Create a list of clickable views
  87. List<View> clickableViews = new ArrayList<>();
  88. clickableViews.add(nativeAdTitle);
  89. clickableViews.add(nativeAdCallToAction);
  90.  
  91. // Register the Title and CTA button to listen for clicks.
  92. nativeAd.registerViewForInteraction(
  93. adView,
  94. nativeAdMedia,
  95. nativeAdIcon,
  96. clickableViews);
  97.  
  98.  
  99. } else if (nativeAd.isAdInvalidated()){
  100. ((MainActivity)mContext).reloadNativeAds();
  101. Toast.makeText((MainActivity)mContext, "nativeAd.isAdLoaded() : "+nativeAd.isAdLoaded()+" isAdInvalidated()", Toast.LENGTH_SHORT).show();
  102.  
  103. }
  104.  
  105. } catch (Exception e){
  106.  
  107. }
  108.  
  109.  
  110. } else {
  111. myHolder.ad_unit.removeAllViewsInLayout();
  112. myHolder.ad_unit.setVisibility(View.GONE);
  113. }
  114.  
  115. }//onBindViewHolder
Add Comment
Please, Sign In to add comment