B4A Library Admob Interstitial Library

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.

B4X:
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.

B4X:
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.
B4X:
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.

B4X:
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
 

Attachments

  • interstitial.png
    interstitial.png
    86 KB · Views: 766
  • AdmobInterstitial1.1.zip
    10.6 KB · Views: 1,043
  • AdmobInterstitialPlayServices.zip
    11.5 KB · Views: 1,051
Last edited:

achtrade

Active Member
Licensed User
Longtime User
Hello,

I've implemented this library and it works fine, but my problem is that the Interstitial is showing each time I change screen, for example, go from the main screen to the setting screen, etc. I would like to show the Interstitial only when the user close the app. How can I do this ?

B4X:
Sub Activity_Create(FirstTime As Boolean)
      'Ad Interstitial
      mwAdInterstitial.Initialize("mwadi","xxxxx")
       mwAdInterstitial.LoadAd
end sub

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

Sub Activity_Pause (UserClosed As Boolean)
      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

Also I tried this but it isn't showing at all:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
      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 if
End Sub
 
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
Hello,

I've implemented this library and it works fine, but my problem is that the Interstitial is showing each time I change screen, for example, go from the main screen to the setting screen, etc. I would like to show the Interstitial only when the user close the app. How can I do this ?

Remove from activity resume

and add this code to your project
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If  KeyCode = KeyCodes.KEYCODE_BACK Then
        Dim result As Int
        result = Msgbox2("Really want to quit?", "Douglas Farias", "OK", "", "Cancel", Null)
        If result = DialogResponse.Positive Then   
        If mwAdInterstitial.Status=mwAdInterstitial.Status_AdReadyToShow Then mwAdInterstitial.Show
        Activity.Finish
        Else
        Return True
        End If
    End If
End Sub
 

achtrade

Active Member
Licensed User
Longtime User
Excellent Douglas, that's what I wanted.

Also, My app is working fine with banner and Interstitial, just in case someone isn't aware.

Thanks
 

Chrrris

Member
Licensed User
Longtime User
Hi,I'm testing this library in an app that runs only in landscape mode (i.e. it has " #SupportedOrientations: landscape" as a Project Attribute in B4A). However, if the App is started on a device with rotation locked to portrait mode in the Android Settings, my app itself runs fine in landscape mode, but all the interstitials are displayed in portrait.

The issue seems to be specific to the wrapper, as other apps that display AdMob interstitials display them correctly in the orientation of the calling app, rather than the orientation that the device is set to.

They display fine (i.e. in landscape, as I'd expect) when screen rotation isn't locked in Android. Is there a fix or workaround?
 

jmon

Well-Known Member
Licensed User
Longtime User
Thanks for this library. I have a few questions though:
  • Is this library still considered as BETA?
  • What are the current limitations / reported crashes?
  • Why isn't there an Anywhere Software created/approved version for the Admob interstitial ads?
Thanks again for sharing!
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
It's no longer beta. I don't see a way to change the thread title.

As for crashes, it can contribute to out of memory errors if you show too many or if your app already uses a lot of memory. Otherwise, it is quite stable.
 

jmon

Well-Known Member
Licensed User
Longtime User
It's no longer beta. I don't see a way to change the thread title.
You can change the title in "Thread tools" next to "Watch thread" at the top right of the page.

Thanks, I will try this library soon!
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Hi,
I wanted to insert AdMob Interstitial in my game, at finish livel when gamer pressed continue button.
I did not understand, if the command used for call the AdMob is: mwAdInterstitial.Show
When this command it's run, nothing AdMob appears.
For initialize mwAdInterstitial.Initialize("mwadi","xxxxxxxxx") I use my AdMob code of my other published App (only AdMob, not Interstitial)..
Where are I wrong?
Thank you.
 
Last edited:

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Dont view because the mwAdInterstitial.Status return 1 and mwAdInterstitial.Status_AdReadyToShow return 2.
Why? :confused:

This is the code when I insert in my game:*



into PROJECT_ATTRIBUTE of Main

B4X:
    #AdditionalRes: C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

into ACTIVITY of main gioco:

B4X:
 Dim mwAdInterstitial As mwAdmobInterstitial
in GLOBAL

B4X:
 mwAdInterstitial.Initialize("mwadi","xxxxxxxx"
mwAdInterstitial.LoadAd
In ACTIVITY_CREATE


and this SUB

B4X:
Sub mwadi_AdLoaded
    Log("ad loaded")
     ToastMessageShow("Pubblicità caricata. Clicca per vederla", True): DoEvents: DoEvents
End Sub

Sub mwadi_AdFailedToLoad (ErrorCode As String)
      ToastMessageShow("Errore pubblicità. Codice errore " & ErrorCode, True): DoEvents: DoEvents
End Sub

When the player press "Continue"
B4X:
Log("mwAdInterstitial.Status=" & mwAdInterstitial.Status)
Log("mwAdInterstitial.Status_AdReadyToShow=" & mwAdInterstitial.Status_AdReadyToShow)
If mwAdInterstitial.Status=mwAdInterstitial.Status_AdReadyToShow Then
   mwAdInterstitial.Show
End If

This is my MANIFEST.XML:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="16"/>

android:installLocation="preferExternal"

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
   '<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)

SetApplicationAttribute(android:largeHeap,"true")
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

'End of default text.
'admob interstitial

AddApplicationText(
<meta-data android:name="com.google.android.gms.version"
  android:value = "@integer/google_play_services_version"/>
<activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
                )
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
What is this google politicy in their questions page?

1. How can I receive CPM or interstitial ads?
Currently we are able to offer CPM banners and interstitials to only a select group of publishers. These publishers are chosen based on their performance on our CPC network, and other factors related to the demand of advertisers.

The team of Google AdMob will go directly to you if your application qualifies for CPM ads or interstitials.

Could this be the problem that I do not see anything? But another my application, has been running a normal AdMob.
Where am I wrong?
Please help me understand.:(
PS: My game is not on google store (it's a working progress).
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
erosmax,

What does your log show? Is the adloaded sub firing?

I initialize/load the interstitial in activity resume instead of activity create. You might try that as well.

Jack
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Mmmmh I had only used the mwAdInterstitial2 library and not have error but not even the AdMob interstitial. If I change it when the mwAdInterstitial 1.10 library, it want the GoogleAdMobAdsSdk-6.4.1.jar into library folder. I've download it but I get this error message (in my game iand in your example app admobinterstitial code posted):

"Ad Failed to load with error code: Ad request successful, but no ad returned due to lack of ad inventory."

I use my ID Code wih work in other my App (but not interstitial, only Admob banner click).

What is this message error? Why the mwAdInterstitial2 not work?
 
Last edited:

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Ok thank you...but in your example app return:

"Ad Failed to load with errore code: No fill"

What is this?
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
Are you getting that with the example that uses play services or the old library?

It means that it is communicating fine with admob, but they don't have any ads to send you.
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
This message appears with then new library (Google Play Service) and in your code example into AdmobInterstitialPlayServices.zip
I'v use my id code for normal Admod (by another my app).
 
Top