B4A Library AdManager Library

Wrapper for AdManager Library: https://developers.google.com/ad-manager/mobile-ads-sdk/android/quick-start
Banner, Interstitial and Rewarded Ads only.

AdManager

Author:
Author: Google - B4a Wrapper: Pendrush
Version: 1.52
  • AdManager
    • Events:
      • AdClicked
      • AdExpanded
      • AdFailedToLoad (Error As String)
      • AdImpression
      • AdLoaded
      • AdOpened
    • Fields:
      • ADSIZE_BANNER As com.google.android.gms.ads.AdSize
        Mobile Marketing Association (MMA) banner ad size (320x50 density-independent pixels).
      • ADSIZE_FULL_BANNER As com.google.android.gms.ads.AdSize
        Interactive Advertising Bureau (IAB) full banner ad size (468x60 density-independent pixels)
      • ADSIZE_LARGE_BANNER As com.google.android.gms.ads.AdSize
        Large banner ad size (320x100 density-independent pixels).
      • ADSIZE_LEADERBOARD As com.google.android.gms.ads.AdSize
        Interactive Advertising Bureau (IAB) leaderboard ad size (728x90 density-independent pixels).
      • ADSIZE_MEDIUM_RECTANGLE As com.google.android.gms.ads.AdSize
        Interactive Advertising Bureau (IAB) medium rectangle ad size (300x250 density-independent pixels).
    • Functions:
      • BringToFront
      • Initialize (EventName As String)
        Initialize Banner Ad
        AdManager1.Initialize("AdManager1")
      • Invalidate
      • Invalidate2 (arg0 As android.graphics.Rect)
      • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • IsInitialized As Boolean
      • LoadNonPersonalized (AdUnitId As String, AdSize As com.google.android.gms.ads.AdSize)
        Loads an Non Personalized ad.
        AdManager1.LoadNonPersonalized("/6499/example/banner", size.Native)
      • LoadPersonalized (AdUnitId As String, AdSize As com.google.android.gms.ads.AdSize)
        Loads an Personalized ad.
        AdManager1.LoadPersonalized("/6499/example/banner", size.Native)
      • Pause
        This method should be called in the parent Activity_Pause method.
      • RemoveView
      • RequestFocus As Boolean
      • Resume
        This method should be called in the parent Activity_Resume method.
      • SendToBack
      • SetBackgroundImage (arg0 As android.graphics.Bitmap) As BitmapDrawable
      • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
      • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
      • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • Properties:
      • Background As android.graphics.drawable.Drawable
      • Color As Int [write only]
      • Enabled As Boolean
      • Height As Int
      • Left As Int
      • Padding As Int()
      • Parent As Object [read only]
      • Tag As Object
      • Top As Int
      • Visible As Boolean
      • Width As Int
  • AdManagerInterstitial
    • Events:
      • AdDismissedFullScreenContent
      • AdFailedToLoad (Error As String)
      • AdFailedToShowFullScreenContent (Error As String)
      • AdLoaded
      • AdShowedFullScreenContent
    • Functions:
      • Initialize (EventName As String, AdUnitId As String)
        Initialize Interstitial Ad.
        AdManagerInterstitial1.Initialize("AdManagerInterstitial1", "/6499/example/interstitial")
      • IsInitialized As Boolean
      • IsLoaded As Boolean
        Returns True if the Ad was successfully loaded and is ready to be shown.
      • LoadNonPersonalized
        Load an Non Personalized ad.
      • LoadPersonalized
        Load an Personalized ad.
      • Show
        Show the Interstitial Ad.
  • AdManagerRewarded
    • Events:
      • AdDismissedFullScreenContent
      • AdFailedToLoad (Error As String)
      • AdFailedToShowFullScreenContent (Error As String)
      • AdLoaded
      • AdShowedFullScreenContent
      • UserEarnedReward (RewardType As String, RewardAmount As Int)
    • Functions:
      • Initialize (EventName As String, AdUnitId As String)
        Initialize Rewarded Ad.
        AdManagerRewarded1.Initialize("AdManagerRewarded1", "ca-app-pub-3940256099942544/5354046379")
      • IsInitialized As Boolean
      • IsLoaded As Boolean
        Returns True if a Rewarded Ad is available and is ready to be shown.
      • LoadNonPersonalized
        Load an Non Personalized ad.
      • LoadPersonalized
        Load an Personalized ad.
      • Show
        Show the Rewarded Ad.
  • MobileAdsSdk
    • Functions:
      • Initialize


Add this in manifest (replace ca-app-pub-3940256099942544~3347511713 with your APP ID):
B4X:
AddApplicationText(
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>

<meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/> 
            <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 
         
            <activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
          android:theme="@android:style/Theme.Translucent.NoTitleBar"
          android:exported="false"/>
            <meta-data
          android:name="com.google.android.gms.version"
          android:value="@integer/google_play_services_version" />
        <receiver
                    android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
                    android:enabled="true"
                    android:exported="false" >
                </receiver>
     
          <service
                    android:name="com.google.android.gms.measurement.AppMeasurementService"
                    android:enabled="true"
                    android:exported="false" />
                 
         <receiver
                    android:name="com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver"
                    android:enabled="true"
                    android:exported="true"
                    android:permission="android.permission.INSTALL_PACKAGES" >
                    <intent-filter>
                        <action android:name="com.android.vending.INSTALL_REFERRER" />
                    </intent-filter>
        </receiver>
        <service
                    android:name="com.google.android.gms.measurement.AppMeasurementJobService"
                    android:enabled="true"
                    android:exported="false"
                    android:permission="android.permission.BIND_JOB_SERVICE" />)


v1.52 new banner sizes for static banners:
  • ADSIZE_BANNER - Mobile Marketing Association (MMA) banner ad size (320x50 density-independent pixels).
  • ADSIZE_FULL_BANNER - Interactive Advertising Bureau (IAB) full banner ad size (468x60 density-independent pixels).
  • ADSIZE_LARGE_BANNER - Large banner ad size (320x100 density-independent pixels).
  • ADSIZE_LEADERBOARD - Interactive Advertising Bureau (IAB) leaderboard ad size (728x90 density-independent pixels).
  • ADSIZE_MEDIUM_RECTANGLE - Interactive Advertising Bureau (IAB) medium rectangle ad size (300x250 density-independent pixels).
 

Attachments

  • AdManagerLibrary.zip
    15.2 KB · Views: 307
  • AdManagerExample.zip
    14.7 KB · Views: 311
Last edited:

tufanv

Expert
Licensed User
Longtime User
Dear Pendrush,

Thanks very much for your support and quick help. Will try this out asap
 

tufanv

Expert
Licensed User
Longtime User
Dear Pendrush,

Rewarded and interstitial works perfect on emulator, but when I click on banner ad I get an error: Maybe because of Emulator ?

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (actinterstitial) Create, isFirst = true **
** Activity (actinterstitial) Resume **
AdLoaded - event fire
** Activity (actinterstitial) Pause, UserClosed = false **
sending message to waiting queue (admanagerinterstitial1_adclosed)
running waiting messages (1)
AdClosed - event fire
** Activity (actinterstitial) Resume **
** Activity (actinterstitial) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (actrewarded) Create, isFirst = true **
** Activity (actrewarded) Resume **
RewardedAdLoaded - event fire
** Activity (actrewarded) Pause, UserClosed = false **
sending message to waiting queue (admanagerrewarded1_rewardedadopened)
sending message to waiting queue (admanagerrewarded1_rewardedadclosed)
running waiting messages (2)
RewardedAdOpened - event fire
RewardedAdClosed - event fire
** Activity (actrewarded) Resume **
** Activity (actrewarded) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (actbanner) Create, isFirst = true **
Error occurred on line: 66 (actBanner)
java.lang.RuntimeException: Method: getCurrentOrientationAnchoredAdaptiveBannerAdSize not found in: com.google.android.gms.ads.AdSize
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
    at b4a.example.actbanner._getadaptiveadsize(actbanner.java:407)
    at b4a.example.actbanner._activity_create(actbanner.java:368)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.example.actbanner.afterFirstLayout(actbanner.java:105)
    at b4a.example.actbanner.access$000(actbanner.java:17)
    at b4a.example.actbanner$WaitForLayout.run(actbanner.java:83)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6680)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
** Activity (actbanner) Resume **




Wrapper for AdManager Library: https://developers.google.com/ad-manager/mobile-ads-sdk/android/quick-start
Banner, Interstitial and Rewarded Ads only.




Add this to manifest:
B4X:
AddApplicationText(<meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>
            <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
     
            <activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
          android:theme="@android:style/Theme.Translucent.NoTitleBar"
          android:exported="false"/>
            <meta-data
          android:name="com.google.android.gms.version"
          android:value="@integer/google_play_services_version" />
        <receiver
                    android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
                    android:enabled="true"
                    android:exported="false" >
                </receiver>
 
          <service
                    android:name="com.google.android.gms.measurement.AppMeasurementService"
                    android:enabled="true"
                    android:exported="false" />
             
         <receiver
                    android:name="com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver"
                    android:enabled="true"
                    android:exported="true"
                    android:permission="android.permission.INSTALL_PACKAGES" >
                    <intent-filter>
                        <action android:name="com.android.vending.INSTALL_REFERRER" />
                    </intent-filter>
        </receiver>
        <service
                    android:name="com.google.android.gms.measurement.AppMeasurementJobService"
                    android:enabled="true"
                    android:exported="false"
                    android:permission="android.permission.BIND_JOB_SERVICE" />)
 

Pendrush

Well-Known Member
Licensed User
Longtime User
Change minSdkVersion to 16 in manifest.
Also use additional resources and SDK Manager from B4a v10.2 Beta and update everything. I'm also made test in Android Studio emulator and everything work fine.
Actually implementation for Adaptive banner is made by Erel on this post: https://www.b4x.com/android/forum/threads/admob-adaptive-banners.111525/post-695517
Also this: https://www.b4x.com/android/forum/threads/adaptive-banners.111544/


emulator.png
 
Last edited:
Top