B4A Library Admob Interstitial Library

Discussion in 'Additional libraries, classes and official updates' started by Jack Cole, Sep 23, 2013.

  1. Jack Cole

    Jack Cole Active Member Licensed User

    Because the old Admob SDK will be deprecated in August, I have written a library for interstitials using the new Google Play Services library. You'll need to follow initial steps noted in Erel's post for the Admob banner library to get your app ready for this library.

    The new library should be compatible with your old code with the exception of a change to the AdFailedToReceive event. It should be declared in your app as follows.

    Code:
    Sub mwadi_AdFailedToLoad (ErrorMessage As String)
        
    Log("failed to load ad: " & ErrorMessage)
    End Sub
    This new library also supports a new event that triggers when the user closes an ad. You can use this event to queue a new ad to load or run other code.

    Code:
    Sub mwadi_AdClosed
        mwAdInterstitial.LoadAd
    End Sub
    The new library is called mwAdmobInterstitial2. I added the 2 to the end of the name so you can preserve your old library in case it is needed with other apps. The new library and example app for the library is attached at the end of this post.

    ============================================
    Below is for the old version (not using Google Play Services)
    ============================================
    I was able to put together a library to display interstitial ads from Admob. You'll need to create an interstitial ad unit in Admob and use it with the initialization process.

    Make sure the following is added to your AndroidManifest.xml.
    Code:
    AddApplicationText(<activity android:name="com.google.ads.AdActivity"
              android:configChanges=
    "keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>)
    Here is code from the included sample app that shows how to use the library. Because the interstitial is running as an activity, I'm not able to raise an event in B4A when it is dismissed. Instead, you can check the status in Activity_Resume as shown in the code below.

    A good practice would be to initialize the ad in Activity_Create, and then show the ad later when desired. This will allow the ad to fully load while other processes are taking place allowing you to show the ad immediately.

    This is a beta version and the first library I've shared, so please report any problems.

    NOTE: There is a bug in older versions of the SDK that causes a crash on some devices when you try to load the interstitial. Make sure you download an updated SDK from Admob.

    1.1 Update: Fixed a bug in the library code used to raise the AdFailedToLoad event. Separated out the LoadAd routine from the Initialize routine. This allows you to load a new ad after an ad has been dismissed without re-initializing the class.

    Code:
    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
        Dim mwAdInterstitial As mwAdmobInterstitial
        
    Dim Button1 As Button
        
    Dim Label2 As Label
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        'Activity.LoadLayout("Layout1")
        mwAdInterstitial.Initialize("mwadi","INSERT_YOUR_AD_UNIT_ID")
        mwAdInterstitial.LoadAd
        
    Activity.LoadLayout("1")
        Label2.Text=
    "Attempting to load ad...  Please wait."
    End Sub

    Sub Activity_Resume
        
    If mwAdInterstitial.Status=mwAdInterstitial.Status_Dismissed Then Label2.Text="Ad dismissed by user."
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub


    Sub mwadi_AdLoaded
        
    Log("ad loaded")
        Label2.Text=
    "Ad loaded - Click Show Ad to Display"

    End Sub

    Sub mwadi_AdFailedToLoad (ErrorCode As String)
        Label2.Text=
    "Ad failed to load with error code: " & ErrorCode
        
    Log("failed to load ad: " & ErrorCode)
    End Sub

    Sub Button1_Click
        
    If mwAdInterstitial.Status=mwAdInterstitial.Status_AdReadyToShow Then mwAdInterstitial.Show
        
    If mwAdInterstitial.Status=mwAdInterstitial.Status_Dismissed Then
            Label2.Text=
    "Attempting to load ad...  Please wait."
            mwAdInterstitial.LoadAd
        
    End If
    End Sub
     

    Attached Files:

    Last edited: May 9, 2014
    Croïd, Scantech, JordiCP and 13 others like this.
  2. jota

    jota Active Member Licensed User

    Thanks for the library, will be very useful.
     
    Last edited: Sep 26, 2013
  3. MaxApps

    MaxApps Active Member Licensed User

    Can anyone run interstitials or is it still by invitation only?
     
  4. Jack Cole

    Jack Cole Active Member Licensed User

    I never received an invite, so I assume it is open for everyone.
     
  5. MaxApps

    MaxApps Active Member Licensed User

    You say:
    How do I do that? I can´t find that anywhere at Admob and I cannot find it in their help
     
  6. MaxApps

    MaxApps Active Member Licensed User

    I figured it out - MEDIATION

    Works well. Thank you for this much needed library :)
     
  7. MaxApps

    MaxApps Active Member Licensed User

    How do I load the interstitial more than one time?
    If re initialize it again, it comes up with an error.
    Is there a reload?
    also if you have already shown the ad and dismissed it, if you then change the oriantation, it come up with error:
     
  8. Jack Cole

    Jack Cole Active Member Licensed User

    I'll check into it. It may be Sunday before I can take a look. I'll probably need to make some changes to the library code.
     
  9. Jack Cole

    Jack Cole Active Member Licensed User

    I have published an update to the library above fixing the problem with raising the AdFailedToLoad event. I have also implemented a separate LoadAd routine so you can call it again to load a new ad after it has been dismissed without having to re-initialize.
     
  10. icebold

    icebold Member Licensed User

    Hi Job Jack,
    thanks for the library you shared.
    I report this error compiling the code filled with my admob pub ID: A referenced library is missing: googleadmobadssdk-6.4.1

    The GoogleAdMobAdsSdk-6.4.1.jar library is copied in my "other lib" path but the example programs doesn't work.
    Do I have to add some more lib ?
     
    Last edited: Nov 28, 2013
  11. Jack Cole

    Jack Cole Active Member Licensed User

    Please verify that all files are in the same folder. I have all mine in the b4a libraries folder.
     
  12. icebold

    icebold Member Licensed User

    Hi Jack,
    all files are in the same folder but I always get this error:
    :confused:
     
  13. Jack Cole

    Jack Cole Active Member Licensed User

    Are you having this problem with the sample app included in the original post?
     
  14. icebold

    icebold Member Licensed User

    Yes. I changed only my pub id
     
  15. Jack Cole

    Jack Cole Active Member Licensed User

    That is a mystery to me. I just tried it out again on my PC and everything is working. I don't have the files in the other libs folder, but in the b4a main libraries folder.
     
  16. icebold

    icebold Member Licensed User

    I also moved the files in the main lib folder but I get the same error.
    I want to test it again on my other pc during the weekend. I will let you know.
    Thanks Jack!
     
  17. Yalçın Kondur

    Yalçın Kondur Member Licensed User

    try making a copy of GoogleAdMobAdsSdk.jar and rename the copy to GoogleAdMobAdsSdk-6.4.1.jar
     
  18. Rorry

    Rorry Member Licensed User

    Hi, thanks for this lib.

    any idea about the compiler error i get:

    thank you for help.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    This error means that two referenced libraries include the same class (com.google.ads.Ad).
    You will need to remove one of those libraries.
     
  20. Rorry

    Rorry Member Licensed User

    ok erel. thank you.
    so we have to choose between banners and Interstitials ads. we cant use the both?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice