B4A Class FirebaseAdmob Native Ad Class

Jack Cole

Active Member
Licensed User
Across the forum, there are pieces of code that you can put together to make native ads work. I put everything together into a class that saves me a lot of time, and hopefully you will find it helpful also.

This example uses a pre-built layout and uses the Unified Native Ad for the type of native ad. Content and install ads are slated to be deprecated, so it would probably be a good idea to update your code to use the unified native ads.

Getting started is pretty simple. Here is the code used in my attached example to load a native ad. Copy the layout file and class to your project. Don't forget to setup Firebase and add the related lines to the app manifest. Also, you need to replace the REPLACE_WITH_YOUR_NATIVE_AD_ID with your native ad id.

One missing feature would be the ability to handle the event for a failure to load the ad. If anyone knows how to handle this, please post some code and I'll update it. If you make improvements to this class, please share!

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 nuad As mwFirebaseAdmobNative
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")
End Sub
Sub Activity_Resume
    Dim testing=True As Boolean
    nuad.Initialize(Me,"REPLACE_WITH_YOUR_NATIVE_AD_ID",10dip,10dip,100%X-20dip,150dip,Activity,testing)
    nuad.LoadUnifiedNativeAd
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    If nuad.IsInitialized Then nuad.RemoveView
End Sub
Private Sub nativead_ReceiveAd
    Log("MAIN-nativead_ReceiveAd")
End Sub
Thank you to Erel and asales. I used some information/code they provided in part of this class.

upload_2019-7-7_12-47-33.png
 

Attachments

Top