public void OnContentAdLoaded(NativeContentAd ad) { //Inflate and store it in a public dicctionary int ad_id = activity.dic_nativeAds.Count; LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater; NativeContentAdView ad_view = (NativeContentAdView)inflater.Inflate(Resource.Layout.NativeAdTemplate, null); activity.dic_nativeAds.Add(ad_id, ad_view); ad_view.AdChoicesView = new AdChoicesView(activity); activity.PopulateContentAdView(ad, ad_view); LinearLayout layout = activity.FindViewById <LinearLayout>(Resource.Id.ad_fake_placeholder); layout.AddView(ad_view); //Prepare Image to temp file to load it from webview string ad_imageFile = ""; IList <NativeAd.Image> ad_images = ad.Images; if (ad_images.Count > 0) { Java.IO.File tempFile = Java.IO.File.CreateTempFile("adimg", ".jpg", activity.ApplicationContext.CacheDir); Bitmap bitmap = ((BitmapDrawable)ad_images[0].Drawable).Bitmap; MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); File.WriteAllBytes(tempFile.AbsolutePath, stream.ToArray()); ad_imageFile = tempFile.AbsolutePath; } //Pass ad information to webview string script = "ad_show(" + ad_id + ", " + "'" + ad.Headline.Replace("'", "\\'") + "', " + "'" + ad_imageFile.Replace("'", "\\'") + "', " + "'" + ad.Body.Replace("'", "\\'") + "', " + "'" + ad.CallToAction.Replace("'", "\\'") + "', " + "'" + ad.Advertiser.Replace("'", "\\'") + "'" + ");"; webView.EvaluateJavascript(script, null); }
public void PopulateContentAdView(NativeContentAd ad, NativeContentAdView adView) { adView.HeadlineView = adView.FindViewById(Resource.Id.nativead_headline); adView.ImageView = adView.FindViewById(Resource.Id.nativead_image); adView.BodyView = adView.FindViewById(Resource.Id.nativead_body); adView.CallToActionView = adView.FindViewById(Resource.Id.nativead_callToAction); adView.LogoView = adView.FindViewById(Resource.Id.nativead_logo); adView.AdvertiserView = adView.FindViewById(Resource.Id.nativead_advertiser); // Some assets are guaranteed to be in every NativeContentAd. ((TextView)adView.HeadlineView).Text = ad.Headline; ((TextView)adView.BodyView).Text = ad.Body; ((TextView)adView.CallToActionView).Text = ad.CallToAction; ((TextView)adView.AdvertiserView).Text = ad.Advertiser; IList <NativeAd.Image> images = ad.Images; if (images.Count > 0) { ((ImageView)adView.ImageView).SetImageDrawable(images[0].Drawable); } // Some aren't guaranteed, however, and should be checked. NativeAd.Image logoImage = ad.Logo; if (logoImage == null) { adView.LogoView.Visibility = ViewStates.Invisible; } else { ((ImageView)adView.LogoView).SetImageDrawable(logoImage.Drawable); adView.LogoView.Visibility = ViewStates.Visible; } // Assign native ad object to the native view. adView.SetNativeAd(ad); }