How to display Google AdMob ads on xamarin.forms project?

钢 屈 371 Reputation points
2021-11-04T13:48:41.287+00:00

How to display Google AdMob ads on xamarin.forms project?
Thank you.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} vote

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-11-05T06:20:23.707+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To display the google Admob ads in Xamarin.Forms, we need to install the required packages for the native platform project: Xamarin.GooglePlayServices.Ads for Android and Xamarin.Firebase.iOS.AdMob for iOS. Then try creating a customView which display the ads and render the ads in native project using custom renderer.

       public class AdsView : View  
       {  
           //you could add some properies to define the ads view  
           public AdsView()  
           {  
               this.BackgroundColor = Color.Accent;  
           }  
       }  
    

    Custom renderer class

       [assembly: ExportRenderer(typeof(AdsView), typeof(AdsViewRenderer))]    
       namespace AdMobTestSample.Droid  
       {  
           public class AdsViewRenderer : ViewRenderer  
           {  
               Context context;  
               public AdsViewRenderer(Context _context) : base(_context)  
               {  
                   context = _context;  
               }  
               protected override void OnElementChanged(ElementChangedEventArgs<View> e)  
               {  
                   base.OnElementChanged(e);  
                   ...  
                   var adView = new AdView(Context);  
                   adView.AdUnitId = "Your AdMob id";  
                   var requestbuilder = new AdRequest.Builder();  
                   adView.LoadAd(requestbuilder.Build());  
                   SetNativeControl(adView);  
               }  
           }  
       }  
    

    For more details, you could search with the keyword as Display Google AdMob Ads In Xamarin Forms to view the related documentation.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. LMiles 21 Reputation points
    2022-02-04T14:35:10.063+00:00

    For a Nov 2021 article, this is useless really.

    The Xamarin.GooglePlayServices.Ads for Android has changed dramatically.

            MobileAds.Initialize(this); THIS does not properly load the application Id from the Manifest data AND an overload needs a listener
            MobileAds.Initialize(this, "ca-app-pub-MY APP ID"); THIS is no longer valid and is deprecated
    

    The Manifest needs this:

    <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-MY APP ID"/>
    

    One needs to add this to the MAinActivity OR into the Renderer

            RequestConfiguration.Builder builder = new RequestConfiguration.Builder();
            builder = builder.SetTestDeviceIds(new string[] { "my device ID" });
            MobileAds.RequestConfiguration = builder.Build();
    

    In a Xamarin.FORMS application, 5 pages, main and 4 child pages... Ads not showing but no errors thrown in code.

    I get the following in output, if anyone can enlighten me on this...that would be most helpful!!

    [Ads] This request is sent from a test device.
    [DynamiteModule] Local module descriptor class for com.google.android.gms.ads.dynamite not found.
    [DynamiteModule] Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:214106404
    [DynamiteModule] Selected remote version of com.google.android.gms.ads.dynamite, version >= 214106404
    [Ads] #4 The webview is destroyed. Ignoring action.

    WHAT IS THIS: com.google.android.gms.ads.dynamite?
    WHY is this breaking, not being found?

    Thanks

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.