issue integrating admob in xamarin android

Vincenzo Ravo 1 Reputation point
2021-04-15T23:05:14.03+00:00

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

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-04-16T02:45:12.04+00:00

    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://learn.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.

    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.