question

VincenzoRavo-1820 avatar image
0 Votes"
VincenzoRavo-1820 asked RobCaplan edited

issue integrating admob in xamarin android

Hi, i'm having some issues trying to get admob to work into my xamarin android, it has no errors in the xml and the cs file but the ads will not show up, it only shows a small black square.

here is my xml code

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:ads="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:showIn="@layout/activity_main">
    
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerInParent="true"
         android:text="Hello World!" />
     <com.google.android.gms.ads.AdView
         android:minWidth="25px"
         android:minHeight="25px"
         android:layout_centerHorizontal="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         ads:adSize="BANNER"
         ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
         android:id="@+id/adView1" />
    
 </RelativeLayout>

my manifest

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
           android:versionCode="1" 
           android:versionName="1.0" 
           package="com.companyname.app1">
   <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
   <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
     <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-0000000000000000~0000000000"/>
   </application>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 </manifest>

and the code behind

 var _adbanner = FindViewById<AdView>(Resource.Id.adView1);
 AdRequest _adrequest = new AdRequest.Builder().Build();
 _adbanner.SetBackgroundColor(Android.Graphics.Color.Black);
 _adbanner.LoadAd(_adrequest);

thanks in advance for all the help

p.s. : sorry for the code mess, i didn't found any code tags to use with the code blocks



dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @VincenzoRavo-1820 ,I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 edited

Hello,


Welcome to Microsoft Q&A!

There are some problems in your code:

1.Please add the neccessasy permission , you didn't add INTERNET permission.

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

2.Please recheck if you have used the right ads:adUnitId .

You need to do is actually register for AdMob. This will give you two important pieces of information, your Application Code and your Ad Unit Id. You can go to: https://apps.admob.com, register for a new app, link to an app in Firebase.

Note: I found you used the ads:adUnitId from official sample AdMobExample Sample .It is not correct.

You can refer document Xamarin.Forms: Google AdMob Ads in Android to implement your app.

Besides, you can also check the offical sample: https://docs.microsoft.com/en-us/samples/xamarin/monodroid-samples/google-services-admobexample/ .



Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.
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.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.