Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ADMob not show in Android - Xamarin forms

leonardoartuso2leonardoartuso2 Member ✭✭
edited February 2019 in Xamarin.Forms

Good Morning,

I Have a solution app in xamarin forms, and I need add ADMOB, and I do this, but, my ad not show, please help me:

My AdMob work:

I use plugin: Xamarin.GooglePlayServices.Ads - Version: 60.1142.1 - the last version.

MainActivity onCreate, My row of initialize MobileAd:
MobileAds.Initialize(ApplicationContext, "ca-app-pub-xxxxxxxxxxxxxxxxx~xxxxxxxxxx"); I tryed with ID: "xxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxx", but I not have success

My ADMobView class:

 **public class AdMobView : View
 {
 }**

My AdMobRenderer:

    ** [assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobViewRenderer))]
     namespace Anuncie.Droid.Renderes
     {
         public class AdMobViewRenderer : ViewRenderer<AdMobView, AdView>
         {
             public AdMobViewRenderer(Context context) : base(context) { }

             string adUnitId = string.Empty;
             //Note you may want to adjust this, see further down.
             AdSize adSize = AdSize.SmartBanner;
             AdView adView;
             AdView CreateNativeAdControl()
             {
                 if (adView != null)
                     return adView;

                 // This is a string in the Resources/values/strings.xml that I added or you can modify it here. This comes from admob and contains a / in it
                 adUnitId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxx"; I tryed with ca-app-pub-xxxxxxxxxxxxxxxxx~xxxxxxxxxx, but I not have success
                 adView = new AdView(Forms.Context);
                 adView.AdSize = adSize;
                 adView.AdUnitId = adUnitId;

                 var adParams = new LinearLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);

                 adView.LayoutParameters = adParams;

                 adView.LoadAd(new AdRequest
                                 .Builder()
                                 .Build());
                 return adView;
             }

             protected override void OnElementChanged(ElementChangedEventArgs<AdMobView> e)
             {
                 base.OnElementChanged(e);
                 if (Control == null)
                 {
                     if (Control == null)
                     {
                         CreateNativeAdControl();
                         SetNativeControl(adView);
                     }
                 }
             }
         }
     }**

Thanks

Answers

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    Please share your .xaml code where you place the AdMobView. And have your tried give the AdMobView a specific height?

  • leonardoartuso2leonardoartuso2 Member ✭✭
    edited February 2019

    @YorkGo said:
    Please share your .xaml code where you place the AdMobView. And have your tried give the AdMobView a specific height?

    Good Morning, I Tryed yes, Width = 320 and Hight = 50, but I Tryed too add in Grid, i Tryed add BackgroundColor = Red, but i don't have Success.

    My AddView.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Anuncie.Page.PageAnuncio"
                 xmlns:local="clr-namespace:Anuncie.Page;assembly=Anuncie">
        <ContentPage.Content>
            <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    
            <local:AdMobView WidthRequest="320" HeightRequest="50"/>
    
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    
  • xam_donjoxam_donjo Member ✭✭

    I had same problem with admob. I set my adunit id in admob control and it started showing ads. You also might want to try out the default testing units to see if the ads are displaying or not. Real ads take time to show.

  • @xam_donjo said:
    I had same problem with admob. I set my adunit id in admob control and it started showing ads. You also might want to try out the default testing units to see if the ads are displaying or not. Real ads take time to show.

    Thanks, you know how many time to I see my ad? In Ad of testing a try and i have Success, my problem is in Ad real :/

  • xam_donjoxam_donjo Member ✭✭

    ad real can be a problem because it depends on advertisers in your location. Also it took me 24 hrs' to see ad in my admob unit (banner ads).

  • MarcoTronconeMarcoTroncone GBUniversity ✭✭✭

    Hi,
    if you have problems with Admob,
    you can try to use my free plugin https://www.nuget.org/packages/MarcTron.Admob.

    It supports Banner, Interstitial and Rewarded Video for Android and iOS and it's incredibly easy to use.

    On my blog you can also find a tutorial: https://www.xamarinexpert.it/blog/admob-made-easy

    If you need help, let me know.

  • @MarcoTroncone said:
    Hi,
    if you have problems with Admob,
    you can try to use my free plugin [ht tps ://w ww .nuget .org/packages/MarcTron.Admob](https ://w ww .nuget. org/packages/MarcTron.Admob "https: //w ww. nuget .org/packages/MarcTron.Admob").

    It supports Banner, Interstitial and Rewarded Video for Android and iOS and it's incredibly easy to use.

    On my blog you can also find a tutorial:

    If you need help, let me know.

    I tried your plugin and i am getting this after i did as you mentioned in your blog -


    • The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers *
    • should follow the instructions here: https:// goo .gl/fQ2neu to add a valid *
    • App ID inside the AndroidManifest. Google Ad Manager publishers should *
    • follow instructions here: https:// goo .gl/h17b6x. *

    and also this -

    Java.Lang.RuntimeException: 'Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException:

    Not sure what is wrong - how i can contact you?

    Regards,
    TJW

  • MarcoTronconeMarcoTroncone GBUniversity ✭✭✭

    @thejumpingwolf said:

    @MarcoTroncone said:
    Hi,
    if you have problems with Admob,
    you can try to use my free plugin [ht tps ://w ww .nuget .org/packages/MarcTron.Admob](https ://w ww .nuget. org/packages/MarcTron.Admob "https: //w ww. nuget .org/packages/MarcTron.Admob").

    It supports Banner, Interstitial and Rewarded Video for Android and iOS and it's incredibly easy to use.

    On my blog you can also find a tutorial:

    If you need help, let me know.

    I tried your plugin and i am getting this after i did as you mentioned in your blog -


    • The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers *
    • should follow the instructions here: https:// goo .gl/fQ2neu to add a valid *
    • App ID inside the AndroidManifest. Google Ad Manager publishers should *
    • follow instructions here: https:// goo .gl/h17b6x. *

    and also this -

    Java.Lang.RuntimeException: 'Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException:

    Not sure what is wrong - how i can contact you?

    Regards,
    TJW

    HI @thejumpingwolf

    Have you tried the Test project here: https://github.com/marcojak/TestMTAdmob

    Anyway your AndroidManifest should be something like this (taken from the TestMTAdmob project):

    <?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.TestMTAdmob"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" /> <application android:label="TestMTAdmob.Android"> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <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>
    Just remember to replace this Test application ID with yours.
    Let me know if this works, if not will see how to fix this issue.
    If it doesn't write me here, or on my blog or add an Issue on the project page: https://github.com/marcojak/MTAdmob

  • thejumpingwolfthejumpingwolf Member ✭✭
    edited October 2019

    @MarcoTroncone said:

    @thejumpingwolf said:

    @MarcoTroncone said:
    Hi,
    if you have problems with Admob,
    you can try to use my free plugin [ht tps ://w ww .nuget .org/packages/MarcTron.Admob](https ://w ww .nuget. org/packages/MarcTron.Admob "https: //w ww. nuget .org/packages/MarcTron.Admob").

    It supports Banner, Interstitial and Rewarded Video for Android and iOS and it's incredibly easy to use.

    On my blog you can also find a tutorial:

    If you need help, let me know.

    I tried your plugin and i am getting this after i did as you mentioned in your blog -


    • The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers *
    • should follow the instructions here: https:// goo .gl/fQ2neu to add a valid *
    • App ID inside the AndroidManifest. Google Ad Manager publishers should *
    • follow instructions here: https:// goo .gl/h17b6x. *

    and also this -

    Java.Lang.RuntimeException: 'Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException:

    Not sure what is wrong - how i can contact you?

    Regards,
    TJW

    HI @thejumpingwolf

    Have you tried the Test project here: https ://github .com/marcojak/TestMTAdmob

    Anyway your AndroidManifest should be something like this (taken from the TestMTAdmob project):

    <?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.TestMTAdmob"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" /> <application android:label="TestMTAdmob.Android"> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <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>
    Just remember to replace this Test application ID with yours.
    Let me know if this works, if not will see how to fix this issue.
    If it doesn't write me here, or on my blog or add an Issue on the project page: https ://github .com/marcojak/MTAdmob

    Ok Marc,

    Edited the manifest and the app crash stopped and it was able to run and deploy on REAL DEVICE not in EMULATOR

    BUT

    new issue here

    Java.Lang.IllegalStateException: 'The ad size and ad unit ID must be set before loadAd is called.' in MainActivity.cs

    **
    Code needed below: **

    protected override void OnCreate(Bundle savedInstanceState)
    {

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
            base.OnCreate(savedInstanceState);
    
            MobileAds.Initialize(ApplicationContext, "ca-app-pub-xxxxxxxxxxxxxxx");
    
    
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
    

    if we add the control in .XAML file like this

    then no issue but the banner in bottom never displays

    again but if we do like this

    then the ad size error comes forward...

    Update: the repo of your is working but the ADs showing is very late and not working on a real device! [ small screen size]

  • Now in DEC latest update the project
    https://github.com/marcojak/MTAdmob

    Not showing any kind of ads. :\

    What is wrong @MarcoTroncone ?
    Do you have any idea?

  • MarcoTronconeMarcoTroncone GBUniversity ✭✭✭
    Ho @thejumpingwolf,

    I would need more information.
    It could be multiple things.

    Have you used the correct ads I'd? (Try with the test I'd, it's better and during development is required by Google)
    Does the ads have enough space to appear? (Especially height).
    Have you initialized correctly admob?
  • @MarcoTroncone said:
    Ho @thejumpingwolf,

    I would need more information.
    It could be multiple things.

    Have you used the correct ads I'd? (Try with the test I'd, it's better and during development is required by Google)
    Does the ads have enough space to appear? (Especially height).
    Have you initialized correctly admob?

    I directly downloaded your repo from github and compiled + installed in real device
    did not work!

    None of them loaded - not banner - not reward - not interstitial etc etc... previously it worked.

    Specifically :

    1. ADs ID is from your repo so i guess was correct? nothing was changed.
    2. It is your repo so i guess it had :P
      1. Again, your repo it was.

    Device : Android 5.0 - screen size 4.5" (480 X 854)

    Point: even if the screen size was issue then wh the other ads type did not load and always shown IsLoaded = False?

    I tried to put as much details as possible.

    Thanks

  • MarcoTronconeMarcoTroncone GBUniversity ✭✭✭
    Are you talking about the Testmtadmob or the MTAdmob itself?

    I have to have a look at the code.
    Meanwhile does the console give you some information?
  • @MarcoTroncone said:
    Are you talking about the Testmtadmob or the MTAdmob itself?

    I have to have a look at the code.
    Meanwhile does the console give you some information?

    Yes. That repor.

    ref: https://www.xamarinexpert.it/admob-made-easy/

    Link here https://github.com/marcojak/MTAdmob

    Situation at my end :


    Please help as things are looking much much complex not sure why.

  • @thejumpingwolf said:

    @MarcoTroncone said:
    Are you talking about the Testmtadmob or the MTAdmob itself?

    I have to have a look at the code.
    Meanwhile does the console give you some information?

    Yes. That repor.

    ref: https://www.xamarinexpert.it/admob-made-easy/

    Link here https://github.com/marcojak/MTAdmob

    Situation at my end :


    Please help as things are looking much much complex not sure why.

    ** i had to edit the manifest and now the error gone but no ads showing up.

  • Working on different phone - screen size

    Android 7.1.xxxxx

    Display 5 inch

    I suspect the issue is the banner width 460 but should be 320

Sign In or Register to comment.