B4A Library AdMob library

Erel

B4X founder
Staff member
Licensed User
Longtime User
Question, why the width and height reports -2 when I do something like this:
How was AdBox initialized? Which value was printed?

So the Adview's size should still be 320dip x 50dip but you can initialize it with a bigger, SIZE_IAB_MRECT dimensions?
No. The AdView size should match the ad size.
 

NJDude

Expert
Licensed User
Longtime User
How was AdBox initialized? Which value was printed?

This is the code:

B4X:
'Activity module

Sub Process_Globals
    
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
   
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
    Dim AdBox As AdView

End Sub

Sub Activity_Create(FirstTime As Boolean)

    AdBox.Initialize2("Ad", "<pub code>", AdBox.SIZE_BANNER)
    Activity.AddView(AdBox, 0dip, Activity.Height - 50dip, AdBox.Width, AdBox.Height)
    AdBox.LoadAd
            
    ToastMessageShow("Width=" & AdBox.Width, True)
    ToastMessageShow("Height=" & AdBox.Height, True)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

I get -2 on Width and Height.

The ads are displayed properly regardless of size.
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
Well, it would be nice if I could get the height by using "AdBox.Height" so I wouldn't have to do something like:

B4X:
Activity.AddView(AdBox, 0dip, Activity.Height - [COLOR="Red"][b]50dip[/b][/COLOR], AdBox.Width, AdBox.Height)

But:

Activity.AddView(AdBox, 0dip, Activity.Height - [COLOR="Red"][b]AdBox.Height[/b][/COLOR], AdBox.Width, AdBox.Height)

But it's ok, is not the end of the world, thanks Erel.
 

Gigatron

Member
Licensed User
Longtime User
Thank you Erel after 8 hour of intense test it's working.

Btw, in the ide it"s is possible to have a addmob cheked button ?
If yes it will auto fill manifest file, and why not update code with admob code on your tutoriel.
if no do nothing, just for this case of admob.



Thanks

(I'am writing english how i think) French is the best language.......
 

jinxaw

Member
Licensed User
Longtime User
Hey,

i am fooling around with the adview right now but i have a problem with the AdScreenDismissed.
I might use it the wrong way...

B4X:
Sub adview2_AdScreenDismissed
   ToastMessageShow("It worked!!!", false)
End Sub

The ToastMessage never shows up.
Can you help me?

Thank you very much!

edit:
Nobody out there who could help me?
Can anyone try to use the AdScreenDismissed?
Please?
Pretty please?
 
Last edited:

aggelos

Member
Licensed User
Longtime User
Hello i have a problem with AdScreenDismissed
i think everything is alright but i can't figure out why its not firing

i am using GoogleAdMobAdsSdkAndroid-4.3.1 and admobs lib 1.2

my xml contains:
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

my program:
Adview1.Initialize("Ad", "XXXXXXXXXXXXX")
Activity.AddView(Adview1, 0dip, Activity.Height - 50dip, 320dip, 50dip)
Adview1.LoadAd

events:
Sub Ad_FailedToReceiveAd (ErrorCode As String)
Log("failed: " & ErrorCode)
End Sub
Sub Ad_ReceiveAd
Log("received")
End Sub
Sub Ad_AdScreenDismissed
ToastMessageShow("aaaaaaaaaaaaaaaaaaaaaaaa!",True)

Log("screen dismissed")
End Sub

receive event is fired when the ad is loaded
i click on the ad.the ad opens and loads in a separate window

the log shows

received (ad loaded ok)
** Activity (main) Pause, UserClosed = false ** (click on the ad)
** Activity (main) Resume ** (click back button to return to the app)

any ideas?


can i also ask if it is possible to implement

public void onPresentScreen(Ad ad)
Called when an Activity is created in front of your app, presenting the user with a full-screen ad UI in response to their touching ad.


thank you
 
Last edited:

aggelos

Member
Licensed User
Longtime User
this is the unfiltered log
i start the program.wait for the ad to load.click it and after the ad loads i click back to return to the program

Start proc com.myprogram.client for activity com.myprogram.client/.main: pid=4594 uid=10124 gids={3003, 1015}
surfaceDestroyed
** Activity (main) Create, isFirst = true **
ignoring event: tbhmain_tabchanged
ignoring event: tbhmain_tabchanged
** Activity (main) Resume **
To get test ads on this device, call adRequest.addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXXXX");
adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_buildAdURL({"preqs":0,"u_sd":1.5,"slotname":"a14f1ce11a77a4c","u_w":320,"msid":"com.myprogram.client","js":"afma-sdk-a-v4.3.1","mv":"8010004.com.android.vending","isu":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","cipa":0,"format":"320x50_mb","net":"ed","app_name":"5.android.com.myprogram.client","hl":"el","u_h":533,"testing":1,"u_audio":1});</script></head><body></body></html>
Displayed com.myprogram.client/.main: +442ms
Can't get the viewWidth after the first layout
Received ad url: <"url": "http://googleads.g.doubleclick.net:80/mads/gma?preqs=0&u_sd=1.5&slotname=a14f1ce11a77a4c&u_w=320&msid=com.myprogram.client&js=afma-sdk-a-v4.3.1&mv=8010004.com.android.vending&isu=7CF31DB787B886053869B19E187C1E32&cipa=0&format=320x50_mb&net=ed&app_name=5.android.com.myprogram.client&hl=el&u_h=533&testing=1&u_audio=1&u_so=p&output=html&region=mobile_app&u_tz=-120&ex=1&client_sdk=1&pto=0&jsv=1", "afmaNotifyDt": "null">
GC_FOR_MALLOC freed 771K, 48% free 3517K/6727K, external 2059K/2137K, paused 151ms
GC_CONCURRENT freed 54K, 48% free 3499K/6727K, external 2059K/2137K, paused 2ms+3ms
nativeDestroy view: 0x361ce0
Can't get the viewWidth after the first layout
GC_EXPLICIT freed 182K, 55% free 3912K/8647K, external 14251K/14581K, paused 73ms
onReceiveAd()
received
GC_CONCURRENT freed 532K, 57% free 2947K/6727K, external 1625K/2137K, paused 6ms+1ms
GC_CONCURRENT freed 528K, 45% free 3818K/6855K, external 2100K/2137K, paused 3ms+2ms
nativeDestroy view: 0x361ee0
Starting: Intent { cmp=com.myprogram.client/com.google.ads.AdActivity (has extras) } from pid 4594
** Activity (main) Pause, UserClosed = false **
onPresentScreen()
ignoring event: adpresentscreen
onLeaveApplication()
Starting: Intent { act=android.intent.action.VIEW dat=http://googleads.g.doubleclick.net/aclk?sa=L&ai=BgtKTNf4mT_WuNYSl8AOMyuXbD7HZjuwB4furpRbAjbcBkE4QARgBIO7o2Rw4AFD-nMW3-_____8BYK0CsgEwY29tLnRvcnJlbnRhbGFybS5jbGllbnQuYWRzZW5zZWZvcm1vYmlsZWFwcHMuY29tugEJMzIweDUwX21iyAEB2gFdaHR0cDovL2NvbS50b3JyZW50YWxhcm0uY2xpZW50LmFkc2Vuc2Vmb3Jtb2JpbGVhcHBzLmNvbS9hMTRmMWNlMTFhNzdhNGM_cz1hMTRmMWNlMTFhNzdhNGMmZz0xgAIBqQKskXAo2wO2PqgDAcgDAugDrwjoA-QB6AP-AvUDAAAAwPUDAAAAEJAGAQ&num=1&cid=5GigGA&sig=AOD64_2tlNWgjyqt9ADLZC6nDwlI4zq9sw&client=ca-mb-app-pub-3698889006637145&adurl=http://www.sequence.gr&nh=1 cmp=com.android.browser/.BrowserActivity } from pid 4594
GC_EXTERNAL_ALLOC freed 87K, 54% free 3123K/6727K, external 2742K/2807K, paused 29ms
Displayed com.android.browser/.BrowserActivity: +279ms (total +357ms)
Starting: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=http://www.sequence.gr/?gclid=CKean_DL-K0CFQlpfAodmlMJvA cmp=com.android.browser/.BrowserActivity } from pid 4068
GC_CONCURRENT freed 503K, 57% free 2958K/6727K, external 1625K/2137K, paused 46ms+2ms
GC_CONCURRENT freed 510K, 57% free 2956K/6727K, external 1625K/2137K, paused 46ms+2ms
GC_CONCURRENT freed 810K, 53% free 3197K/6727K, external 3121K/3858K, paused 4ms+6ms
No keyboard for id 131073
Using default keymap: /system/usr/keychars/qwerty.kcm.bin
Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@408aa4e0 (uid=10124 pid=4594)
Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4081c1c0
onDismissScreen()
ignoring event: ad_adscreendismissed
** Activity (main) Resume **
Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@408c5ed8 (uid=10050 pid=4068)
Displayed com.myprogram.client/com.google.ads.AdActivity: +39s746ms
nativeDestroy view: 0x514c98
GC_EXPLICIT freed 130K, 55% free 3071K/6727K, external 2247K/2807K, paused 59ms
 

aggelos

Member
Licensed User
Longtime User
the events work now

but onPresentScreen() is shown in the wrong time
I mean the onPresentScreen() should show when the user clicks the ad
and just before the ad shows.
instead i see this in the log

** Activity (main) Pause, UserClosed = false **
onPresentScreen()
onLeaveApplication()
sending message to waiting queue.
onDismissScreen()
sending message to waiting queue.
running waiting messages (2)
Present screen 16:21:50
screen dismissed16:21:50
** Activity (main) Resume **

if its not possible for present screen to fire before the app pauses i guess
i could use activity_pause with screen dismissed.

thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you like you can update the library. It is pretty simpler:
B4X:
package anywheresoftware.b4a.admobwrapper;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.DontInheritEvents;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.objects.ViewWrapper;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

/**
 * The AdMob library allows you to add ads served by AdMob to your application.
 *See the <link>AdMob Tutorial|http://www.b4x.com/forum/basic4android-getting-started-tutorials/7300-admob-tutorial-add-ads-your-application.html</link>.
 *This library requires some additional configuration as described in the tutorial.
 *By default the view must be set to a size of 320dip x 50dip. Otherwise it will not display.
 *Tablets support three additional sizes: IAB_BANNER - 468dip x 60dip, IAB_MRECT - 300dip x 250dip and IAB_LEADERBOARD - 728dip x 90dip.
 *The size of AdView must match the selected size.
 */
@Version(1.35f)
@ShortName("AdView")
@Events(values={"ReceiveAd", "FailedToReceiveAd (ErrorCode As String)",
      "AdScreenDismissed", "PresentScreen"})
@ActivityObject
@DontInheritEvents
@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
@DependsOn(values={"GoogleAdMobAdsSdk"})
public class AdViewWrapper extends ViewWrapper<AdView> {
   /**
    * 320dip x 50dip (default size)
    */
   public static Object SIZE_BANNER = AdSize.BANNER;
   /**
    * 468dip x 60dip - tablet only
    */
   public static Object SIZE_IAB_BANNER = AdSize.IAB_BANNER;
   /**
    * 728dip x 90dip - tablet only
    */
   public static Object SIZE_IAB_LEADERBOARD = AdSize.IAB_LEADERBOARD;
   /**
    * 300dip x 250dip - tablet only
    */
   public static Object SIZE_IAB_MRECT = AdSize.IAB_MRECT;
   /**
    * Initializes the AdView using the default 320dip x 50dip size.
    *EventName - Name of Subs that will handle the events.
    *PublisherId - The publisher id you received from AdMob.
    */
   public void Initialize(final BA ba, String EventName, String PublisherId) {
      Initialize2(ba, EventName, PublisherId, AdSize.BANNER);
   }
   /**
    * Initializes the AdView.
    *EventName - Name of Subs that will handle the events.
    *PublisherId - The publisher id you received from AdMob.
    *Size - One of the SIZE constants.
    */
   public void Initialize2(final BA ba, String EventName, String PublisherId, Object Size) {
      setObject(new AdView(ba.activity, (AdSize)Size, PublisherId));
      super.Initialize(ba, EventName);
      final String eventName = EventName.toLowerCase(BA.cul);
      getObject().setAdListener(new AdListener() {

         @Override
         public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode e){
            ba.raiseEvent(getObject(), eventName + "_failedtoreceivead", e.toString());
         }
         @Override
         public void onReceiveAd(Ad ad) {
            ba.raiseEvent(getObject(), eventName + "_receivead");
         }
         @Override
         public void onDismissScreen(Ad arg0) {
            ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_adscreendismissed", false, null);
         }
         @Override
         public void onLeaveApplication(Ad arg0) {
            //
         }
         @Override
         public void onPresentScreen(Ad arg0) {
            ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_presentscreen", false, null);

         }
      });
   }

   /**
    * Sends a request to AdMob, requesting an ad.
    */
   public void LoadAd() {
      AdRequest req = new AdRequest();
      req.setTesting(true);
      
      getObject().loadAd(req);
   }

}
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…