Sub LoadNativeAd
Private adCount As Int = 0
Private AdUnitId As String = "ca-app-pub-3940256099942544/2247696110"'
Private ctxt As JavaObject
Private builder As JavaObject
ctxt.InitializeContext
builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
Private onAppInstallAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeAppInstallAd.OnAppInstallAdLoadedListener".Replace("/", "."), _
"ContentAdLoaded", Null)
builder.RunMethod("forAppInstallAd", Array(onAppInstallAdLoadedListener))
Private OnContentAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeContentAd.OnContentAdLoadedListener".Replace("/", "."), _
"ContentAdLoaded", Null)
builder.RunMethod("forContentAd", Array(OnContentAdLoadedListener))
Private Listener As JavaObject
Listener.InitializeNewInstance(Application.PackageName & ".main$MyAdListener", Array("NativeAd")) 'change 'main' with the current activity module name
builder.RunMethod("withAdListener", Array(Listener))
Private AdLoader As JavaObject = builder.RunMethod("build", Null)
Private AdRequestBuilder As JavaObject
AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
AdLoader.RunMethod("loadAds", Array(AdRequestBuilder.RunMethod("build", Null), 2))
Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)
adCount = 1
Private NativeContentAdView As JavaObject
NativeContentAdView.InitializeNewInstance("com/google/android/gms/ads/formats/NativeContentAdView".Replace("/", "."), _
Array(ctxt))
pnlNativeAdView = NativeContentAdView
Private content As Panel
content.Initialize("")
Activity.AddView(content, 0, 0, 100%x, 100%y)
content.LoadLayout("contentad")
initAdViews
content.RemoveView
pnlNativeAdView.AddView(content, 0, 0, 100%x, 100%y)
NativeContentAdView.RunMethod("setNativeAd", Array(setupAd(Args(0), NativeContentAdView, adCount - 1)))
Do While AdLoader.RunMethod("isLoading", Null)
Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)
adCount = adCount + 1
NativeContentAdView.RunMethod("setNativeAd", Array(setupAd(Args(0), NativeContentAdView, adCount - 1)))
Log("Received an Ad")
Loop
If adCount = 1 Then adPanel(1).Visible = False
End Sub
Private Sub initAdViews
Private adPanel() As Panel = Array As Panel(pnlAd, pnlAd2)
Private adHeadline() As Label = Array As Label(lblAdHeadline, lblAdHeadline2)
Private adBody() As Label = Array As Label(lblAdBody, lblAdBody2)
Private adImage() As Panel = Array As Panel(pnlAdImage, pnlAdImage2)
Private adAction() As Button = Array As Button(btnAdAction, btnAdAction2)
End Sub
Private Sub setupAd(adContent As JavaObject, nativeContentAdView As JavaObject, thisAd As Int) As JavaObject
adHeadline(thisAd).Text = getTruncatedString(adContent.RunMethod("getHeadline", Null), adHeadline(thisAd))
adBody(thisAd).Text = adContent.RunMethod("getBody", Null)
Dim images As List = adContent.RunMethod("getImages", Null)
If images.IsInitialized And images.Size > 0 Then
Log("adding image")
Dim image As JavaObject = images.Get(0)
Log(image.RunMethod("getScale", Null))
adImage(thisAd).Background = image.RunMethod("getDrawable", Null)
nativeContentAdView.RunMethod("setImageView", Array(adImage(thisAd)))
End If
Dim cd As ColorDrawable
cd.Initialize(Colors.RGB(76,190,153),3dip)
adAction(thisAd).Background = cd
adAction(thisAd).Text = adContent.RunMethod("getCallToAction", Null)
nativeContentAdView.RunMethod("setCallToActionView", Array(adAction(thisAd)))
Return adContent
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("Ad Received")
adReady = True
End Sub
Private Sub NativeAd_AdClosed
adReady = False
End Sub
Private Sub getTruncatedString(text As String, thisLabel As Label) As String
Private sRetString As String
Private cvsText As Canvas
Private fMaxLen As Float
Private fStringLen As Float
Dim iCnt As Int
cvsText.Initialize(Activity)
If cvsText.MeasureStringWidth(text, Typeface.DEFAULT, thisLabel.TextSize) <= (thisLabel.Width) Then Return text
fMaxLen = thisLabel.Width - cvsText.MeasureStringWidth("ZZZ", Typeface.DEFAULT, thisLabel.TextSize)
For iCnt = 0 To text.Length - 1
sRetString = text.SubString2(0, iCnt)
fStringLen = cvsText.MeasureStringWidth(sRetString, Typeface.DEFAULT, thisLabel.TextSize)
If fStringLen >= fMaxLen Or (fStringLen = Activity.Width And iCnt = text.Length - 1) Then
Exit
End If
Next
If iCnt < text.Length - 1 Then sRetString = sRetString & "..."
Return sRetString
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