How to implement GoogleAdMob in Xamarin forms ?

Here are step by step  instructions to implement google AdMob through Xamarin Forms.

I have used Test keys of Google AdMob for practice, visit this page to get test keys of sample ads


You can create your own Ads from Google AdMob website, and use generated keys.

I have also this project in Github to download Github project of google admob, don't forget to give star.

1) Create new project in Visual Studio, choose last options for new project.



2) Choose Project type as Xamarin forms, install Xamarin in visual studio if you haven't.
   
    choose Blank(empty) template for Xamarin project.



3) Now we will have Solution Explorer like this, 
    
    here, GoogleAdMob is my project name. And project is setup for both IOS and Android.



4) Now install a NuGet package in Shared project, right click on first project and choose 

    "Manage Nuget Packages".



5) Browse and Install "MarcTron.AdMob" package.
    
    


6) In Solution explorer, right click on shared project and create a new folder  

    



7) I have named folder 'Utilities'
    
    After creating Utilities folder, right click on it and choose 'Add' > 'New Item'.



8) Choose 'Resource File' as type of file you want to add.

    I have given 'StringReference' name of a file.



9)  double click on 'StringReference' file after creating it to open.

    I have filled 
different types of Google Ads Sample test keys in this file, 



10) getting sample test key of google Ads, below is the link to get test keys of Google AdMob

11) Design main.xaml page of project



12) now in  MainPage.xaml.cs file,



Also import these packages on top of 'MainPage.xaml.cs' file to avoid errors,



13) Now we open Android project and configure for Android mobiles.

    Expand Properties 
    
    And double click on  'AndroidManifest.xml' file to open.
 


14) Under <application></application> tag
    
    


add this, copy from here. 

<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.ads.AD_MANAGER_APP"
android:value="true"/>



15) should like this after saving.




16) now open MainActivity.cs file under Android project



17) add this line inside onCreate() method, after Xamarin.forms. init()

    "MobileAds.Initialize(ApplicationContext, "App Id");"




Also don't forget to use this package in this file to avoid error



18) Now Right click on  GoogleAdMob.IOS project,

    Select  "Manage Nuget Packages" to install Nuget package for IOS config.

     Install all these packages under IOS project



19) Open AppDelegate.cs  file of IOS project

paste this code in last,

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            MobileAds.SharedInstance.Start(CompletionHandler);
            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }

        private void CompletionHandler(InitializationStatus status)
        {
            
        }




20)  Right click on info.plist file and select view code.
    
add this code in last,

<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>
<key>GADIsAdManagerApp</key>
<true/>





Results of Solutions would be like this,

























When I will click on "show interstitial Ad" button, this Ad will show

 


And this video Ad will come when I will click on "show video ad"



I have also this project in Github to see code/download visit Github project of google admob, don't forget to give star.




Thanks

Ankit Yadav

Comments

Popular posts from this blog

Nilkanth dham swaminarayan temple, Poicha Gujarat visit

How to solve "Running script disabled/Execution policy" error in Angular CLI ?