Click here to Skip to main content
14,828,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I been working on this for past 3 weeks and I have zero idea why its not showing up.

When I run this code... banner ad doesn't shows up. What am I doing wrong here?

What I have tried:

project folder structure
<pre>
Discount_Calculator
    Controls
        AdControlview.cs
    MainPage.xaml
    ...
Discount_Calculator.Android
    Helper
        AdViewRenderer.cs
    ...
...




AdViewRenderer.cs
C#
[assembly: ResolutionGroupName("Discount_Calculator")]
[assembly: ExportEffect(typeof(AdViewRenderer), "AdControlview")]
namespace Discount_Calculator.Droid.Helper
{
    public class AdViewRenderer : ViewRenderer
    {
        Context context;
        public AdViewRenderer(Context _context) : base(_context)
        {
            context = _context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement == null)
            {
                var adView = new AdView(Context);
                switch ((Element as Discount_Calculator.Controls.AdControlview).Size)
                {
                    case Discount_Calculator.Controls.AdControlview.Sizes.Standardbanner:
                        adView.AdSize = AdSize.Banner;
                        break;
                    case Discount_Calculator.Controls.AdControlview.Sizes.LargeBanner:
                        adView.AdSize = AdSize.LargeBanner;
                        break;
                    case Discount_Calculator.Controls.AdControlview.Sizes.MediumRectangle:
                        adView.AdSize = AdSize.MediumRectangle;
                        break;
                    case Discount_Calculator.Controls.AdControlview.Sizes.FullBanner:
                        adView.AdSize = AdSize.FullBanner;
                        break;
                    case Discount_Calculator.Controls.AdControlview.Sizes.Leaderboard:
                        adView.AdSize = AdSize.Leaderboard;
                        break;
                    case Discount_Calculator.Controls.AdControlview.Sizes.SmartBannerPortrait:
                        adView.AdSize = AdSize.SmartBanner;
                        break;
                    default:
                        adView.AdSize = AdSize.Banner;
                        break;
                }//end switch
                 // TODO: change this id to your ad-unit-id  
                adView.AdUnitId = "ca-app-pub-423232323232323231";
                var requestbuilder = new AdRequest.Builder();
                adView.LoadAd(requestbuilder.Build());
                SetNativeControl(adView);
            } //end if
        }//end func
    }//end class
}//end namespace



AdControlview.cs
namespace Discount_Calculator.Controls
{
    public class AdControlview : Xamarin.Forms.View
    {
        public enum Sizes { Standardbanner, LargeBanner, MediumRectangle, FullBanner, Leaderboard, SmartBannerPortrait }

        public Sizes Size { get; set; }

        public AdControlview()
        {
            this.BackgroundColor = Color.Accent;
        }
    }
}



MainPage.xaml
<ContentPage ...>
<StackLayout>
<Controls:AdControlview Size="Standardbanner" VerticalOptions="EndAndExpand"  />
    </StackLayout>
</ContentPage>


AndroidManifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="2" android:versionName="2.0" package="com.wolfTech.discount_calculator" android:installLocation="auto">
	<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
  
	<application android:label="Discount_Calculator.Android">
		<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" 
               android:value="ca-app-pub-76543245678567~1212345354" />
		<activity android:name="com.google.android.gms.ads.AdActivity" 
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
              android:theme="@android:style/Theme.Translucent" />
	</application>
  
</manifest>
Posted
Updated 2-Jan-20 6:46am
v2
Comments
Kris Lantz 2-Jan-20 15:16pm
   
Is the adView.AdUnitId assignment intentional? The number looks fake and does not match what Google provides here https://developers.google.com/admob/android/banner
From what I could gather, the adLoad() should be called in the class that is going to make the request, and not in the renderer itself, but my experience with how Xamarin does things vs Android Studio, is limited.

1 solution

Please check below link.
it provides a very good example

Xamarin.Android - AdMobExample Sample - Code Samples | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900