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))
Dim OnContentAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeContentAd.OnContentAdLoadedListener".Replace("/", "."), _
"ContentAdLoaded", Null)
builder.RunMethod("forContentAd", Array(OnContentAdLoadedListener))
Dim Listener As JavaObject
Listener.InitializeNewInstance(Application.PackageName & ".main$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)))
Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)
Log("ContentAdLoaded_Event")
Dim NativeContentAd As JavaObject = Args(0)
Log(NativeContentAd.RunMethod("getHeadline", Null))
Dim NativeContentAdView As JavaObject
NativeContentAdView.InitializeNewInstance("com/google/android/gms/ads/formats/NativeContentAdView".Replace("/", "."), _
Array(ctxt))
Dim pNativeAdView As Panel = NativeContentAdView
Dim content As Panel
content.Initialize("")
pNativeAdView.AddView(content, 0, 0, 300dip, 300dip)
Dim lbl As Label
lbl.Initialize("")
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.Text = NativeContentAd.RunMethod("getBody", Null)
content.AddView(lbl2, 10dip, 60dip, 300dip, 50dip)
NativeContentAdView.RunMethod("setBodyView", Array(lbl2))
Dim logo As JavaObject = NativeContentAd.RunMethod("getLogo", Null)
If logo.IsInitialized Then
Log("adding logo")
Dim logoView As Panel
logoView.Initialize("")
logoView.Background = logo.RunMethod("getDrawable", Null)
content.AddView(logoView, 200dip, 0, 100dip, 100dip)
NativeContentAdView.RunMethod("setLogoView", Array(logoView))
End If
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, 200dip, 100dip, 100dip, 100dip)
NativeContentAdView.RunMethod("setImageView", Array(imgView))
End If
NativeContentAdView.RunMethod("setNativeAd", Array(NativeContentAd))
Activity.AddView(pNativeAdView, 0, 0, 100%x, 200dip)
End Sub
Sub NativeAd_FailedToReceiveAd (ErrorCode As String)
Log("NativeAd_FailedToReceiveAd: " & ErrorCode)
End Sub
Sub NativeAd_AdOpened
Log("NativeAd_AdOpened")
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