Android Question Collapsible banner ads

hayk

Member
Hi everyone! I received an Email from Google about new ad type, called Collapsible banner ads. Check this.
Does someone know, How to use Collapsible banner ads?

 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Use B4XPages.
2. Add this to the end of the page:
B4X:
#if java
import android.os.Bundle;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;
public Object GetCollapsibleRequest(String placement) {
    AdRequest.Builder builder = new AdRequest.Builder();
    Bundle extras = new Bundle();
    extras.putString("collapsible", placement);
    builder.addNetworkExtrasBundle(AdMobAdapter.class, extras);
    return builder;
}
#End If

3.
B4X:
Dim request As AdRequestBuilder = Me.As(JavaObject).RunMethod("GetCollapsibleRequest", Array("bottom"))
BannerAd.LoadAdWithBuilder(request)
 
Upvote 0

asales

Expert
Licensed User
Longtime User
also check this post
If you check this post, you will that I don't get an answer to the problem and I could not fix it.

The banner of page2 (b4xpage formt) is still showing when I close the page. This is not happens if I create the page2 as activity format.

If you solve this, please, share the soluction.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Looks like it creates a dialog and adds it to the activity. All pages share the same activity.
There is no API to close it.
I'm getting the dreaded "no fill" error right now, so can't test it. You might be able to find the close button and programmatically click it (I'm not sure whether this is allowed by Google or not).
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Looks like it creates a dialog and adds it to the activity. All pages share the same activity.
Yes, I think that's it. I found this information:
Collapsible banner ads require you to pass an Activity context
There is no API to close it.
I'm getting the dreaded "no fill" error right now, so can't test it. You might be able to find the close button and programmatically click it (I'm not sure whether this is allowed by Google or not).
I don't found how to close or collapse it automatically and I don't think so is allowed by Google because they are very restricted with the ads now.
I tried several methods (RemoveView, Pause, Resume, Visible = False) without success.
 
Upvote 0

hayk

Member
Is there any chance to make it work properly without B4XPages?
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Is there any chance to make it work properly without B4XPages?
I think it works better with activity instead b4xpages (like I said above).
But due the problem that still showing after close the page, I gave up with this format.
 
Upvote 0

hayk

Member
I think it works better with activity instead b4xpages (like I said above).
But due the problem that still showing after close the page, I gave up with this format.
I use this code:

B4X:
        AdViewCollapsible.Initialize2("AdViewCollapsible","ca-app-pub-3940256099942544/2014213617", AdaptiveSize.Get("native")) ' test colapsable ad
        Activity.AddView(AdViewCollapsible, 0, 100%y - AdaptiveSize.Get("height") , AdaptiveSize.Get("width"), AdaptiveSize.Get("height"))
'        Dim builder As AdRequestBuilder = Me.As(JavaObject).RunMethod("GetCollapsibleRequestBottom", Null)  'baixo para cima
        Dim builder As AdRequestBuilder
        builder.Initialize
        builder = Me.As(JavaObject).RunMethod("GetCollapsibleRequest", Array("bottom"))
'        builder = Me.As(JavaObject).RunMethod("GetCollapsibleRequestBottom", Null)


        AdViewCollapsible.LoadAdWithBuilder(builder)

But get this error:
error:
(IllegalArgumentException) java.lang.IllegalArgumentException: Expected receiver of type com.goldtest.Gold.priece.gold.test.main, but got java.lang.Class<com.goldtest.Gold.priece.gold.test.main>

can't make it work in activity. something is wrong with "builder" I guess
 
Upvote 0

hayk

Member
Hi everyone. I solved the problem, now Collapsible banner ads can be used in Activity without B4XPages. As a base, I used the code example of @asales from this post and modified it.

AdViewCollapsible:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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 AdViewCollapsible As AdView
    Type AdSize (Native As Object, Width As Int, Height As Int) 'there is another option using AdsHelper class
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")
    Activity.Title = "Activity Collapsible Banner"
      
    Dim size As AdSize = GetAdaptiveAdSize
    AdViewCollapsible.Initialize2("AdViewCollapsible", "ca-app-pub-3940256099942544/2014213617", size.Native)


    Dim jo As JavaObject
    jo.InitializeContext
    Dim builder As AdRequestBuilder = jo.RunMethod("GetCollapsibleRequest", Array("bottom"))
    Activity.AddView(AdViewCollapsible, 0, 100%y - size.Height, size.Width, size.Height)
    AdViewCollapsible.LoadAdWithBuilder(builder)
End Sub

Sub Activity_Resume
    ' Place any code here that needs to run when the activity resumes
    AdViewCollapsible.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    AdViewCollapsible.Pause
End Sub

'Adaptive Banners
Sub GetAdaptiveAdSize As AdSize
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject
    Dim width As Int = 100%x / GetDeviceLayoutValues.Scale
    Dim res As AdSize
    res.Native = AdSize.InitializeStatic("com.google.android.gms.ads.AdSize").RunMethod("getCurrentOrientationAnchoredAdaptiveBannerAdSize", Array(ctxt, width))
    Dim jo As JavaObject = res.Native
    res.Width = jo.RunMethod("getWidthInPixels", Array(ctxt))
    res.Height = jo.RunMethod("getHeightInPixels", Array(ctxt))
    Return res
End Sub

#If Java
import android.os.Bundle;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;

public AdRequest.Builder GetCollapsibleRequest(String placement) {
    AdRequest.Builder builder = new AdRequest.Builder();
    Bundle extras = new Bundle();
    extras.putString("collapsible", placement);
    builder.addNetworkExtrasBundle(AdMobAdapter.class, extras);
    return builder;
}
#End If


I hope this will be helpfull
 
Upvote 1
Cookies are required to use this site. You must accept them to continue using the site. Learn more…