Android Ads

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,229
Today I couldn't get my personal account to show ads, but that's probably a separate issue, the demo does show.

I made a slim version that includes the knowledge of this whole thread, and also converts it into Mojo2.
(for the record, it behaves exactly the same under Mojo1)

Code:
' Google Admob test ads example

' App description as it appears :
' Starting with black screen, says disabled, loading the first banner (takes a sec)
' 1) showing banner (top left) (still says disabled)
' 2) showing banner (top middle (says enabled)
' 3) showing fullscreen (interstitial) (says disabled just before being shown)
' 4) showing banner (bottom middle) (says disabled)
' Then nothing (continuing saying disabled, it and uses 0 traffic from now on, stuck here)

#ANDROID_LOGCAT_OPTION="-s [Cerberus]:E"

Import mojo2
Import brl.admob

Function Main()
    New MyApp
End

#ADMOB_ANDROID_ADS_APPID = "ca-app-pub-3940256099942544~~3347511713" ' App id.
#ADMOB_PUBLISHER_ID = "ca-app-pub-3940256099942544/6300978111"       ' Banner-ad id.
 
Class MyApp Extends App

    Field admob:Admob
    Field interstitialAdmob:AdmobInterstitial
    Field layout:Int = 1 ' Layouts 1 - 6
    Field state:Int = 1
    Field canvas:Canvas

    Method OnCreate()
        canvas = New Canvas()
        admob=Admob.GetAdmob()
        admob.ShowAdView 1,layout
        interstitialAdmob = AdmobInterstitial.GetAdmobInterstitial("ca-app-pub-3940256099942544/1033173712") ' Interstitial-ad id.
        SetSwapInterval 1 ; SetUpdateRate 0
    End

  Method OnUpdate()
    If MouseHit( 0 )
      If state = 3
        admob.HideAdView
        state+=1
      Else If state = 1
        layout+=1
        If layout=7 layout=1
        admob.ShowAdView 1,layout
        state+=1
      Else If state = 2
        admob.HideAdView
         interstitialAdmob.ShowAdViewInterstitial
        state+=1
      End
    End
  End
 
    Method OnRender()
        Local en:="disabled"
        If state=2 en="enabled"
        canvas.Clear
        canvas.PushMatrix
        canvas.Scale 2,2
        canvas.DrawText "Click to toggle ads! ads are currently "+en,DeviceWidth/4,DeviceHeight/4,.5,.5
        canvas.PopMatrix
        canvas.Flush
    End
 
    Method OnResume:Int() ' Resume from interstitial-ad.
        admob.ShowAdView 1,5
        Return 0
    End Method
 
End
 
Last edited:

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,229
It was just a logic problem, this code shows all the layouts and loops correctly

It will show top banners, bottom banners, fullscreen ad, and centred banner, and disabled mode


Code:
Import mojo2
Import brl.admob

Function Main()
    New MyApp
End

#ADMOB_ANDROID_ADS_APPID = "ca-app-pub-3940256099942544~~3347511713" ' App id.
#ADMOB_PUBLISHER_ID = "ca-app-pub-3940256099942544/6300978111" ' Banner-ad id.
   
Class MyApp Extends App

    Field admob:Admob
    Field interstitialAdmob:AdmobInterstitial
    Field layout:Int = 1 ' Layouts 1 - 7, no states needed.
    Field canvas:Canvas

    Method OnCreate()
        canvas = New Canvas()
        admob=Admob.GetAdmob()
        admob.ShowAdView 1,layout ' Show initial ad
        interstitialAdmob=AdmobInterstitial.GetAdmobInterstitial("ca-app-pub-3940256099942544/1033173712") ' Interstitial-ad id.
        SetSwapInterval 1 ; SetUpdateRate 0
    End

  Method OnUpdate()
     
    If MouseHit(0)
 
        layout = layout + 1 ; If layout > 8 Then layout = 0
         
        ' Possible banner-ad layouts
        ' -----------------------------
        ' 1 = Left top
        ' 2 = Centre top
        ' 3 = Right top
        ' 4 = Left bottom
        ' 5 = Centre bottom
        ' 6 = Right bottom
        ' 7 = Centre
     
         ' Banner-ad with all layouts top/bottom
            If layout >= 1 And  layout <= 6    
            admob.HideAdView
            admob.ShowAdView 1,layout
          Endif
   
          ' Interstitial ad
          If layout = 7
            admob.HideAdView ; interstitialAdmob.ShowAdViewInterstitial
         Endif
 
        ' When closing the Interstitial OnResume is called where we create a centred Banner ad.
     
        ' Disabled ads
        If layout = 0
             admob.HideAdView
        Endif

    EndIf
 
 End  

    Method OnRender()
        Local en:="disabled"
        If layout >= 1 And layout <= 6 Then en="enabled (banner)"
        If layout = 7 Then en="enabled (interstitial)"
        If layout = 8 Then en="enabled (banner)"
        If layout = 0 Then en="disabled"
        canvas.Clear
        canvas.PushMatrix
        canvas.Scale 2,2
        canvas.DrawText "Ads are currently "+en,DeviceWidth/4,DeviceHeight/4,.5,.5
        canvas.PopMatrix
        canvas.Flush
    End

    Method OnResume:Int() ' Resume from interstitial-ad.
        admob.HideAdView
        admob.ShowAdView 1,7 ' Show a centred banner.
        layout = 8
        Return 0
    End Method
 
End
 
Last edited:

MikeHart

Administrator
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,390
Btw I compiled the github sourcecode and interstitialAdmob.ShowAd still gives "Identifier 'ShowAd' not found" I forgot what I had to change to get passed it.

Found the mod needed for the example :

changed interstitialAdmob.ShowAd
into
interstitialAdmob.ShowAdViewInterstitial
Well, not sure were YOU got your source from, but that was definitely not in the develop branch of the CX repository.
 
Last edited:

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
705
This is the right one. Just make sure you choose the "develop" branch with the button in the top left corner.
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,229
Okay! I always downloaded the "develop" version. Actually I did not know that you could choose among several versions.
I now see there's an Admob test version and something else.. But yes this is from the latest development version of Cerberus.

I found the example code in docs/html/Examples and I used as a template here and for all my tests. It works great!
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
705
I found the example code in docs/html/Examples and I used as a template here and for all my tests. It works great!
That could be the cause of the problem. These are just links to the files in the examples folder of your cerberus installation.
It is best to look there directly. admob is in the mojo/mak folder
Man, I have to get the docs fixed soon. This week I have some time to get some things done there.
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,229
Oh I thought those examples where old. Does that mean that IAP works too? Thanks.
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
705
Does that mean that IAP works too?
I don't know. Just try it and report back how it goes.
At the moment I don't have any Apps in the stores to test everything properly.
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,229
It was the same exact example with the correct command in place already. Maybe hte logic is better working or maybe not. But the exmaple gave here has a complete walk through of all possible ad positions and fullscreen ad and it works in a loop. So I didn't miss anything.

But I'm interested in that AdMob branch now.
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
705
Well, that branch was recently added to give us a space for admob related experiments.
I will try your example. Would you mind if I use some of your code for the official example?
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,229
Of course by all means. It adds a 7:th layout (screen-centred banner).

The code could use some cleaning up because I miss-used the layout variable as a state too in the two higher numbers.
 
Top Bottom