#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.
Private clv2 As CustomListView
Private Label1 As B4XView
Private CheckBox1 As B4XView
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("Layout_native_ad")
For i = 1 To 40
If i=5 Or i=10 Or i=15 Or i=20 Or i=25 Then
' clv2.AddTextItem("fffffffffffffff",i)
LoadNativeAd
Else
clv2.Add(CreateListItem($"Item #${i}"$, clv2.AsView.Width, 60dip), $"Item #${i}"$)
End If
Next
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub LoadNativeAd
Dim AdUnitId As String = "ca-app-pub-3940256099942544/2247696110"
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim builder As JavaObject
builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
'*** Code changed
Dim onAppInstallAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeAppInstallAd.OnAppInstallAdLoadedListener".Replace("/", "."), _
"AppInstallAdLoaded", Null)
builder.RunMethod("forAppInstallAd", Array(onAppInstallAdLoadedListener))
'*** End Code changed
Dim Listener As JavaObject
Listener.InitializeNewInstance(Application.PackageName & ".nativeactivity$MyAdListener", Array("NativeAd")) 'change 'main' with the current activity module name
builder.RunMethod("withAdListener", Array(Listener))
Dim AdLoader As JavaObject = builder.RunMethod("build", Null)
Dim AdRequestBuilder As JavaObject
AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
'*** Code changed
Wait For AppInstallAdLoaded_Event (MethodName As String, Args() As Object)
'*** End Code changed
Log("ContentAdLoaded_Event")
Dim NativeContentAd As JavaObject = Args(0)
Log(NativeContentAd.RunMethod("getHeadline", Null))
Dim NativeContentAdView As JavaObject
'*** Code changed
NativeContentAdView.InitializeNewInstance("com/google/android/gms/ads/formats/NativeAppInstallAdView".Replace("/", "."), _
Array(ctxt))
'*** End Code changed
Dim pNativeAdView As Panel = NativeContentAdView
pNativeAdView.Color=Colors.Cyan
Dim content As Panel
content.Initialize("")
pNativeAdView.AddView(content, 0, 0, 50%x, 300dip)
content.Color=Colors.Gray
Dim lbl As Label
lbl.Initialize("")
lbl.TextSize = 20
lbl.TextColor = Colors.Black
lbl.Color=Colors.Green
lbl.Text = NativeContentAd.RunMethod("getHeadline", Null)
content.AddView(lbl, 10dip, 10dip, 300dip, 50dip)
NativeContentAdView.RunMethod("setHeadlineView", Array(lbl))
Dim lbl2 As Label
lbl2.Initialize("")
lbl2.TextColor = Colors.Black
lbl2.Color = Colors.Blue
lbl2.Text = NativeContentAd.RunMethod("getBody", Null)
content.AddView(lbl2, 10dip, 60dip, 300dip, 50dip)
NativeContentAdView.RunMethod("setBodyView", Array(lbl2))
'*** Code changed (disable) -> show error: java.lang.RuntimeException: Method: getLogo not found in: com.google.android.gms.internal.zzqh
Dim logo As JavaObject = NativeContentAd.RunMethod("getIcon", Null)
If logo.IsInitialized Then
Dim logoView As Panel
logoView.Initialize("")
logoView.Background = logo.RunMethod("getDrawable", Null)
content.AddView(logoView, 260dip, 100dip, 80dip, 80dip)
NativeContentAdView.RunMethod("setIconView", Array(logoView))
End If
'*** End Code changed
Dim images As List = NativeContentAd.RunMethod("getImages", Null)
If images.IsInitialized And images.Size > 0 Then
Log("adding image")
Dim imgView As Panel
imgView.Initialize("")
Dim image As JavaObject = images.Get(0)
imgView.Background = image.RunMethod("getDrawable", Null)
content.AddView(imgView, 130dip, 100dip, 100dip, 100dip)
NativeContentAdView.RunMethod("setImageView", Array(imgView))
End If
'*** Code changed: new button to action
Dim btAction As Button
btAction.Initialize("")
Dim cd As ColorDrawable
cd.Initialize(Colors.RGB(65,105,225),3dip)
btAction.Background = cd
btAction.Text = NativeContentAd.RunMethod("getCallToAction", Null)
content.AddView(btAction, 10dip, 120dip, 100dip, 50dip)
NativeContentAdView.RunMethod("setCallToActionView", Array(btAction))
NativeContentAdView.RunMethod("setNativeAd", Array(NativeContentAd))
pNativeAdView.Color=Colors.Yellow
Dim p As Panel
p.Initialize("")
p.SetLayout(0, 0, 95%x, 30%y)
p.AddView(pNativeAdView,3%x,1%y,95%x,30%y)
' Dim xui As XUI
' Dim ppp As B4XView =xui.CreatePanel("")
clv2.Add(pNativeAdView,1)
End Sub
Sub NativeAd_FailedToReceiveAd (ErrorCode As String)
Log("NativeAd_FailedToReceiveAd: " & ErrorCode)
End Sub
Sub NativeAd_AdOpened
Log("NativeAd_AdOpened")
End Sub
Sub NativeAd_ReceiveAd
Log("1111111111111-NativeAd_ReceiveAd")
End Sub
Sub UnifiedNativeAd_FailedToReceiveAd (ErrorCode As String)
Log("UnifiedNativeAd_FailedToReceiveAd: " & ErrorCode)
End Sub
Sub UnifiedNativeAd_AdOpened
Log("UnifiedNativeAd_AdOpened")
End Sub
Sub UnifiedNativeAd_ReceiveAd
Log("UnifiedNativeAd_ReceiveAd")
End Sub
'
#if Java
public static class MyAdListener extends com.google.android.gms.ads.AdListener {
String eventName;
public MyAdListener(String s) {
eventName = s.toLowerCase(BA.cul);
}
@Override
public void onAdClosed() {
processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adclosed", false, null);
}
@Override
public void onAdFailedToLoad(int arg0) {
processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_failedtoreceivead", false, new Object[] {String.valueOf(arg0)});
}
@Override
public void onAdLeftApplication() {
processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adleftapplication", false, null);
}
@Override
public void onAdOpened() {
processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adopened", false, null);
}
@Override
public void onAdLoaded() {
processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_receivead", false, null);
}
}
#End If
Sub ScrollView1_ScrollChanged(Position As Int)
End Sub
Sub clv2_ItemClick(Index As Int, Value As Object)
clv2.AsView.BringToFront
Log(Index & " = " & Value)
End Sub
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
Dim p As Panel
p.Initialize("")
p.SetLayout(0, 0, Width, Height)
p.LoadLayout("CellItem")
Label1.Text = Text
Return p
End Sub
Sub CheckBox1_CheckedChange(Checked As Boolean)
' Dim index As Int = clv2.GetItemFromView(Sender)
' Dim pnl As B4XView = clv2.GetPanel(index)
' Dim chk As B4XView = pnl.GetView(2)
' MsgboxAsync($"Item value: ${clv2.GetValue(index)}
'Check value: ${chk.Checked}"$, "")
End Sub
Sub Button1_Click
' Dim index As Int = clv2.GetItemFromView(Sender)
' Dim pnl As B4XView = clv2.GetPanel(index)
' Dim lbl As B4XView = pnl.GetView(0)
' Dim chk As B4XView = pnl.GetView(2)
' lbl.Text = "Clicked!"
' 'Msgbox("Item value: " & clv2.GetValue(index) & CRLF & "Check value: " & chk.Checked, "")
' Dim checkedItems As List
' checkedItems.Initialize
' For i = 0 To clv2.GetSize - 1
' Dim p As B4XView = clv2.GetPanel(i)
' Dim chk As B4XView = p.GetView(2)
' If chk.Checked Then
' checkedItems.Add(clv2.GetValue(i))
' End If
' Next
' Log("Checked items: " & checkedItems) 'ignore
End Sub