Java Question How get class as view

Douglas Farias

Expert
Licensed User
Longtime User
Hi all.

How can I initialize a class as a view?
I didn't understand the logic...

Example (Admob by @Erel )
B4X:
    Private BannerAd As AdView
    BannerAd.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", BannerAd.SIZE_SMART_BANNER)
    Dim height As Int
    If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
        'phones
        If 100%x > 100%y Then height = 32dip Else height = 50dip
    Else
        'tablets
        height = 90dip
    End If
    Activity.AddView(BannerAd, 0dip, 100%y - height, 100%x, height)
    BannerAd.LoadAd

BannerAd already is a view, and can be added to the activity normally.

B4X:
Sub Class_Globals
    Private StartAppSDK As JavaObject
    Private ctxt As JavaObject
    Public isReady As Boolean = False
    Private BannerAdListener As Object
    Private mCallback As Object
    Private mEventName As String
    Private bannerAd As JavaObject
    Private mSize As String
End Sub


'SUPPORTED BANNER SIZES (320x50, 728x90, 468x60, 300x50, 480x32 and 1200x627)
Public Sub Initialize(Callback As Object, EventName As String, AppKey As String, testMode As Boolean, size As String)
    mCallback = Callback
    mEventName = EventName
    mSize = size
    
    'INITIALIZE CONTEXT
    ctxt.InitializeContext
    
    'INITIALIZE SDK
    StartAppSDK.InitializeStatic("com.startapp.sdk.adsbase.StartAppSDK")
    StartAppSDK.RunMethod("init", Array(ctxt,AppKey,True))
    StartAppSDK.RunMethod("setTestAdsEnabled", Array(testMode))
    
    'INITIALIZE BANNER
    bannerAd.InitializeNewInstance("com.startapp.sdk.ads.banner.Banner", Array(ctxt))
    BannerAdListener = bannerAd.CreateEventFromUI("com.startapp.sdk.ads.banner.BannerListener", "BannerAdListener", Null)
    bannerAd.RunMethod("setBannerListener", Array(BannerAdListener))
End Sub


Private Sub BannerAdListener_Event (MethodName As String, Args() As Object) As Object
    Select MethodName

        Case "onReceiveAd"
            Args(0) '>BANNER HERE
            If SubExists(mCallback, mEventName & "_ReceiveAd") Then CallSub(mCallback, mEventName & "_ReceiveAd")

        Case "onFailedToReceiveAd"
            If SubExists(mCallback, mEventName & "_FailedToReceiveAd") Then CallSub(mCallback, mEventName & "_FailedToReceiveAd")

        Case "onClick"
            If SubExists(mCallback, mEventName & "_adClicked") Then CallSub(mCallback, mEventName & "_adClicked")
            
            
    End Select
    Return Null
End Sub

Public Sub loadAd
    bannerAd.RunMethod("loadAd", Null)
End Sub

however I was confused by the startapp sdk.
you need start the sdk
you need load the ad
and only after receiving the response onReceiveAd (as view) do you really have a view to display.

my question is, how did erel create a view without first calling loadAd?

I intend to do like erel, add the bannerview in the activity, however to add I have to have the view that is in the onReceivedAd event only.

How can I start a view before it is loaded?
I tried to create a view
private v as view

however, there is no function to initialize.

thxx
 

Douglas Farias

Expert
Licensed User
Longtime User
BannerAd is a native view by itself. If this is not the case with StartApp then implement it as a custom view and add the banner to mBase.
ok what is the correct way to make this?
there is no option to start an empty view.

B4X:
Private v As View
v.initialize '<<<
Activity.AddView(v, 0dip, 100%y - 50dip, 100%x, 50dip)

send the v to class (already started).

the problem is that in the class I only have the banner as view when the onReceiveAd event is called, it takes a while to be called, before that I should have the view already initialized.
How can I start the view before arriving on onReceiveAd?

thank you
 

DonManfred

Expert
Licensed User
Longtime User
You can create a Panel and add it to the Activity (or another Panel) and add the AdView when it arrives in the Event to your Panel.
 
Top