Xamarin Android apply Google AdMob

Ibrar Tariq 20 Reputation points
2024-01-07T21:56:36.4233333+00:00

I am trying to add Google AdMob in my Xamarin Android C# project.

and following this video, the link is given below.

https://www.youtube.com/watch?v=WvZlOI9u2Sc&t=2s&ab_channel=KEISoft

     and The nugget package is

Xamarin.GooglePlayServices.Ads

     and the version I tried 

(42.1021.1) and (122.3.0.2)

Screenshot 2024-01-07 214614

Screenshot 2024-01-07 214702

English

The given error is

1:) ads:adSize="BANNER"

2:) ads:adUnitId= "@string/AdMobID"

3:) adView=FiendViewById<AdView>(Resource.Id.adView);

here is full code

    <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_conterHorizontal="true"
    android:layout_alignParentBottom="true"
   	ads:adSize="BANNER"
    ads:adUnitId="@string/AdMobID"
/>
 protected AdView adView;
 protected override void OnCreate(Bundle savedInstanceState)
 {
     base.OnCreate(savedInstanceState);
     Xamarin.Essentials.Platform.Init(this, savedInstanceState);
     // Set our view from the "main" layout resource
     SetContentView(Resource.Layout.activity_main);

     adView = FindViewById<AdView>(Resource.Id.adView);
     var adRequest = new AdRequest.Builder().Build();
     adView.LoadAd(adRequest);
}

  protected override void OnResume()
  {
      base.OnResume();
      if(adView != null)
      {
          adView.Resume();
      }
  }

Please solve my problem as soon as possible.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,651 Reputation points Microsoft Vendor
    2024-01-08T01:42:00.6233333+00:00

    Hello,

    You can simply add xmlns:ads property to your AdView element, like:

    
    
    
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
    
        xmlns:ads="http://schemas.android.com/apk/res-auto"
    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_conterHorizontal="true"
        android:layout_alignParentBottom="true"
           app:adSize="BANNER"
        app:adUnitId="@string/AdMobID"
    />
    
    

    Then open the strings.xml file in Resources/values/. Then add following AdMobID key and value is your id from google console.

    <resources>
        <string name="app_name">AppADMob</string>
        <string name="action_settings">Settings</string>
    
    
        <string name="AdMobID">your id</string>
    
    
       
    </resources>
    

    In the end, rebuild your project, this error will disappear.

    Best Regards,

    Leon Lu


    If the answer is the right solution, 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.