Android Code Snippet Auto height to banner inline adaptative

This code (created with a help of ChatGPT) return the auto height to use in the banner inline adaptive.

Admob Example updated.
B4X:
Sub GetAutoHeightBannerSize As AdInlineSize
    LogColor("GetAutoHeightBannerSize", Colors.Blue)

    Dim ctxt As JavaObject
    ctxt.InitializeContext

    Dim AdSizeJO As JavaObject
    AdSizeJO.InitializeNewInstance("com.google.android.gms.ads.AdSize", Array(-1, -2)) ' FULL_WIDTH, AUTO_HEIGHT

    Dim res As AdInlineSize
    res.Initialize
    res.Inline = AdSizeJO

    res.Width = AdSizeJO.RunMethod("getWidthInPixels", Array(ctxt))
    res.Height = AdSizeJO.RunMethod("getHeightInPixels", Array(ctxt))

    LogColor("AutoHeight Width (px): " & res.Width, Colors.Green)
    LogColor("AutoHeight Height (px): " & res.Height, Colors.Cyan)

    Return res
End Sub
Usage:
B4X:
Dim sizein As AdInlineSize = Utils.GetAutoHeightBannerSize
Log(sizein.Height)
Log(sizein.Width)
 

nyinyi

Member
I use this Code

B4X:
Sub Globals
Private banner As AdView
Private sv As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)

sv.Initialize(1000dip)
Activity.AddView(sv,0,0,100%x,100%y)

Dim screenwidth as int=100%x/GetDeviceLayoutValues.Scale
If screenwidth>=380 then
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject
    Dim Adwidth As Int = screenwidth-60
    Dim Native As JavaObject = AdSize.InitializeStatic("com.google.android.gms.ads.AdSize").RunMethod("getCurrentOrientationInlineAdaptiveBannerAdSize", Array(ctxt, Adwidth))

banner.Initialize2("banner", "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx",Native)
sv.panel.addview(banner,0,0,sv.width,0)
sv.panel.height=banner.top+banner.height

Else if screenwidth>=320 then

Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject
    Dim Adwidth As Int = 320
Dim Native As JavaObject = AdSize.InitializeStatic("com.google.android.gms.ads.AdSize").RunMethod("getCurrentOrientationInlineAdaptiveBannerAdSize", Array(ctxt, Adwidth))

banner.Initialize2("banner", "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx",Native)
sv.panel.addview(banner,0,0,sv.width,0)
sv.panel.height=banner.top+banner.height

Else

'Not Show Ads'

End Sub

End Sub

Sub Activity_Resume
If banner.IsInitialize then banner.resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If banner.IsInitialize then banner.pause
End Sub

Sub banner_ReceiveAd
 Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject=banner
Log(AdSize.RunMethod("getWidthInPixels", Array(ctxt)))
Log(AdSize.RunMethod("getHeightInPixels",Array(ctxt)))

'To setlayout BannerView'

End Sub
 
Last edited:
Top