Android Question Can use this code for ads intended to be displayed inside scrollable content ?

nyinyi

New Member
B4X:
Sub Process_Globals
    Type AdSize (Native As Object, Width As Int, Height As Int)
    Private xui As XUI
End Sub

Sub Globals
    Dim content1,content2 As Label
    Dim sv As ScrollView
    Dim banner As AdView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    sv.Initialize(1000dip)
    sv.Panel.Color=Colors.LightGray
    Activity.AddView(sv,0,0,100%x,100%y)
    
    content1.Initialize("")
    content1.Text="content1"
    content1.Color=Colors.Red
    content1.Gravity=Gravity.CENTER
    content1.TextColor=Colors.White
    sv.Panel.AddView(content1,10dip,10dip,sv.Width-20dip,50dip)
    
    Dim size As AdSize = GetAdaptiveAdSize
    banner.Initialize2("BannerAd", "ca-app-pub-3940256099942544/9214589741", size.Native)
    banner.Color=Colors.Black
    sv.Panel.AddView(banner,(sv.Width/2)-(size.Width/2),(content1.Top+content1.Height)+10dip,size.Width,size.Height)
    banner.LoadAd
    
    content2.Initialize("")
    content2.Text="content2"
    content2.Color=Colors.Blue
    content2.TextColor=Colors.White
    content2.Gravity=Gravity.CENTER
    sv.Panel.AddView(content2,10dip,(banner.Top+banner.Height)+10dip,sv.Width-20dip,50dip)
    
    sv.Panel.Height=content2.Top+content2.Height+10dip
    
End Sub

Sub Activity_Resume
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub BannerAd_ReceiveAd
    Dim size As AdSize = GetAdaptiveAdSize
    banner.SetLayout((100%x/2)-(size.Width/2),(content1.Top+content1.Height)+10dip,size.Width,250dip)
    content2.SetLayout(10dip,(banner.Top+banner.Height)+10dip,sv.Width-20dip,50dip)
    sv.Panel.Height=content2.Top+content2.Height+10dip
End Sub

Sub BannerAd_FailedToReceiveAd (ErrorCode As String)
    Log(ErrorCode)
End Sub

Sub BannerAd_PresentScreen
    Log("PresentScreen")
End Sub

Sub GetAdaptiveAdSize As AdSize
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject
    Dim width As Int = 300dip/GetDeviceLayoutValues.Scale
    Dim res As AdSize
    res.Native = AdSize.InitializeStatic("com.google.android.gms.ads.AdSize").RunMethod("getCurrentOrientationInlineAdaptiveBannerAdSize", 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
 
Top