Hello friends
I'm going to set AdMob Native ads in my app.
These ads must be displayed after each panel in the scroll but only at the end of the scroll is displayed
where is the problem ?
please guide me
Thank you
I'm going to set AdMob Native ads in my app.
These ads must be displayed after each panel in the scroll but only at the end of the scroll is displayed
where is the problem ?
please guide me
Thank you
B4X:
#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 ScrollView1 As ScrollView
Dim top As Int = 3%y
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")
ScrollView1.Color=Colors.Red
ScrollView1.Panel.Color=Colors.DarkGray
ScrollView1.Panel.Height=400%y
Dim top As Int = 3%y
For i=0 To 15
If i=2 Or i=4 Or i=6 Or i=8 Then
LoadNativeAd(ScrollView1.Panel)
Else
Dim p As Panel
p.Initialize("p")
' p.Color=Colors.ARGB(255,Rnd(0,255),Rnd(0,255),Rnd(0,255))
p.Color=Colors.ARGB(150,0,150,150)
ScrollView1.Panel.AddView(p,3%x,top,ScrollView1.Width-6%x,20%y)
top=top+20%y+3%y
End If
Next
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub LoadNativeAd(pan As Panel)
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
pan.AddView(pNativeAdView,3%x,top,ScrollView1.Width-6%x,30%y)
top=top+30%y+3%y
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
Attachments
Last edited: