Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Is it possible to add a banner advertising to my xamarin.forms app?

I could not find a solution for banner implementation. The api from admob does not work for windows phone or uwp. Also i can not add a banner via admob or anything with the pcl project. The banner will be shown always at the bottom of the app. I would like to develop the ui in xaml in the pcl project. I found a discussion with a custom renderer. But it was not a banner advertising. Is it possible to add a advertising in a xamarin.forms app for all plattforms or at least for android and apple?

Answers

  • kamaeleonkamaeleon DEMember

    Hi
    THX very much! I will try the posted code. I hope it works for apple. ;)

  • kamaeleonkamaeleon DEMember

    Sorry, i have tested the referenced steps for android but it does not work. I had problems with the nuget versioning for the xamarin components.

    I use currently Xamarin.GooglePlayServices.Ads 29.0.0.2 and Xamarin.Forms 2.3.3.180. Another combination does not work with the ads component.

    I added the admobview to the pcl project, added it to the pcl xaml file, updated the manifest in the android project, set the project settings up to 1G, added the admobrenderer in the android project, the build works and the emulator starts, deploy starts and than the error -> Xamarin.Forms.Xaml.XamlParseException: Position 58:2. Type AdMobView not found in xmlns http://xamarin.com/schemas/2014/forms

    I don´t know why it does not work. :/

  • batmacibatmaci DEMember ✭✭✭✭✭

    @kamaeleon did you find a good solution for it? especially for uwp?

  • kamaeleonkamaeleon DEMember

    @batmaci No, sorry! This is not good for Xamarin.Forms. This is not funny. Many problems with nuget dll compatibility first and at the end, it doesn´t exists a working solution for advertising and xamarin.forms and crossplattform development. :(

  • batmacibatmaci DEMember ✭✭✭✭✭
    You are talking about Windows uwp or general? Because admob works fine on Android for me. Did you have problem also on Android using renderers
  • kamaeleonkamaeleon DEMember

    @batmaci
    Nuget compatability -> general :s

    Yes, i have problems with using renderers. I work with Xaml.
    Wich libraries and wich version for admob are you using?

  • batmacibatmaci DEMember ✭✭✭✭✭

    I am also using xaml. I didnt implementing it. I have the latest stable version of XF ad 29.0.02 google services

  • kamaeleonkamaeleon DEMember

    OK... I have a running advertising in my android app. :)
    I reinstalled my complete windows system and installed all visual studio and xamarin components new. :/

    But I don´t know what´s wrong with my iOS app. The advertising was not shown. I get no errors and if I debug the custom renderer everything was OK. I use this code:

    ``
    [assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobRenderer))]
    namespace myApp.iOS
    {
    public class AdMobRenderer : ViewRenderer
    {
    const string AdmobID = "myIOSKey";
    BannerView adView;
    bool viewOnScreen;

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
    {
      base.OnElementChanged(e);
      if (e.NewElement == null)
        return;
      if (e.OldElement == null)
      {
        adView = new BannerView(AdSizeCons.SmartBannerPortrait)
        {
          AdUnitID = AdmobID,
          RootViewController = UIApplication.SharedApplication.Windows[0].RootViewController
        };
        adView.AdReceived += (sender, args) =>
        {
          if (!viewOnScreen) this.AddSubview(adView);
          viewOnScreen = true;
        };
        adView.LoadRequest(Request.GetDefaultRequest());
        base.SetNativeControl(adView);
      }
    }
    

    }
    }
    ``

  • kamaeleonkamaeleon DEMember

    I found a working sample solution for Android and iOS.
    https://github.com/sharmapranav/Xamarin

    Now I´m searching for a solution with WindowsPhone or UWP ;)

  • Richy_GeorgeRichy_George INMember ✭✭✭

    @kamaeleon

    I follow that sample ; in android I'm getting the ads but not in iOS, do i miss something ?

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
  • ionixjuniorionixjunior USMember ✭✭✭
    edited August 2017

    @NguyenLiem RewardedAds is a kind of AdMob? https://developers.google.com/admob/android/rewarded-video

    @kamaeleon you found some implementation for UWP?

  • lightdmlightdm USMember

    @kamaeleon said:
    I found a working sample solution for Android and iOS.

    Now I´m searching for a solution with WindowsPhone or UWP ;)

    work for xamarin native or xamarin forms ?

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    Try this YouTube

    It's using Microsoft Advertising SDK and Google AdMob

  • LearnEverythingLearnEverything USMember ✭✭✭

    On Android Side.

    Manifest

    <activity android:name="com.google.android.gms.ads.AdActivity"
                android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
                android:theme="@android:style/Theme.Translucent" />
    
        <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-9045308AFRJ19031~5857688579"/>
    

    Code C#

    var adView = new AdView(this);
                adView.AdSize = AdSize.SmartBanner;
    #if DEBUG
                //Test Ad
                adView.AdUnitId = "ca-app-pub-3940256099942544/6300978111";
    #else
                 adView.AdUnitId = "ca-app-pub-9046876543518976/1606225012";
    #endif
                var paramss = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
                paramss.AddRule(LayoutRules.AlignParentBottom);
                paramss.AddRule(LayoutRules.CenterHorizontal);
                adView.LayoutParameters = paramss;          
                var layout = FindViewById<RelativeLayout>(Resource.Id.mainRootLayout);
                layout.AddView(adView);
                var adRequest = new AdRequest.Builder().Build();
                adView.LoadAd(adRequest);
    
Sign In or Register to comment.