Android Question [Solved] Native Ads with MediaView - FirebaseAdmob2 and Mobile Ads SDK v20

asales

Well-Known Member
Licensed User
I'm trying to update this code (base in this code) to use the native ads with the new lib FirebaseAdmob2 and new Mobile SDK (20).

The problem is in this part of the code below, that check if the ad has a video and, if don't, show an image.
Native ads with video has a better revenue than an ad with only image.
B4X:
    'check if ad has a video
    Dim vid As JavaObject = NativeAd.RunMethod("getVideoController", Null)
   
    If vid.IsInitialized Then
        Dim MediaView As JavaObject
        MediaView.InitializeNewInstance("com/google/android/gms/ads/formats/MediaView".Replace("/", "."), Array(ctxt))
        content.AddView(MediaView, 10dip, 120dip, 200dip, 200dip)
        NativeAdView.RunMethod("setMediaView", Array(MediaView))
    Else
        'If don't has video, check if is there are images
        Dim images As List = NativeAd.RunMethod("getImages", Null)
        If images.IsInitialized And images.Size > 0 Then
            Dim MediaView As JavaObject
            MediaView.InitializeNewInstance("com/google/android/gms/ads/formats/MediaView".Replace("/", "."), Array(ctxt))
            content.AddView(MediaView, 10dip, 120dip, 200dip, 200dip)
            NativeAdView.RunMethod("setImageView", Array(MediaView))
        End If
    End If
I get this error (and I don't know what has changed in the new sdk to this method "getVideoController"):
B4X:
main$ResumableSub_LoadNativeAdresume (java line: 548)
java.lang.RuntimeException: Method: getVideoController not found in: com.google.android.gms.internal.ads.zzatp
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:363)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
    at br.com.as2.testes.main$ResumableSub_LoadNativeAd.resume(main.java:548)
B4X:
Sub LoadNativeAd
    Dim AdUnitId As String = "ca-app-pub-3940256099942544/2247696110"  'Native Advanced TEST
'    Dim AdUnitId As String = "ca-app-pub-3940256099942544/1044960115"  'Native Advanced with Video TEST
   
    Dim ctxt As JavaObject
    ctxt.InitializeContext
 
    Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
 
    Dim onUnifiedAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/nativead/NativeAd.OnNativeAdLoadedListener".Replace("/", "."), _
       "UnifiedAdLoaded", Null)
    builder.RunMethod("forNativeAd", Array(onUnifiedAdLoadedListener))
 
    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))
 
'    Sends a request To AdMob, requesting an ad.
    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 (builder) UnifiedAdLoaded_Event (MethodName As String, Args() As Object)
   
    Dim NativeAd As JavaObject = Args(0)
   
    LogColor("LoadNativeAd", Colors.Green)
    Log("getHeadline: " & NativeAd.RunMethod("getHeadline", Null))
    Log("getBody: " & NativeAd.RunMethod("getBody", Null))
    Log("getAdvertiser: " & NativeAd.RunMethod("getAdvertiser", Null))
    Log("getCallToAction: " & NativeAd.RunMethod("getCallToAction", Null))
    Log("getPrice: " & NativeAd.RunMethod("getPrice", Null))
    Log("getStarRating: " & NativeAd.RunMethod("getStarRating", Null))
    Log("getStore: " & NativeAd.RunMethod("getStore", Null))
    Log("getExtras: " & NativeAd.RunMethod("getExtras", Null))
 
    Dim NativeAdView As JavaObject
     
    NativeAdView.InitializeNewInstance("com/google/android/gms/ads/nativead/NativeAdView".Replace("/", "."), _
       Array(ctxt))  
           
    Dim pNativeAdView As Panel = NativeAdView
 
    Dim content As Panel
    content.Initialize("")
    pNativeAdView.AddView(content, 0, 0, 100%x, 300dip)
 
    Dim lbl As Label
    lbl.Initialize("")
    lbl.TextSize = 20
    lbl.TextColor = Colors.Black
    lbl.Text = NativeAd.RunMethod("getHeadline", Null)
    content.AddView(lbl, 10dip, 10dip, 300dip, 50dip)
    NativeAdView.RunMethod("setHeadlineView", Array(lbl))
 
    Dim lbl2 As Label
    lbl2.Initialize("")
    lbl2.TextColor = Colors.Black
    lbl2.Text = NativeAd.RunMethod("getBody", Null)
    content.AddView(lbl2, 10dip, 60dip, 300dip, 50dip)
    NativeAdView.RunMethod("setBodyView", Array(lbl2))
 
    Dim logo As JavaObject = NativeAd.RunMethod("getIcon", Null)
    If logo.IsInitialized Then
        Dim logoView As Panel
        logoView.Initialize("")
        logoView.Background = logo.RunMethod("getDrawable", Null)
        content.AddView(logoView, 250dip, 10dip, 80dip, 80dip)
        NativeAdView.RunMethod("setIconView", Array(logoView))
    End If
   
    'check if ad has a video
    Dim vid As JavaObject = NativeAd.RunMethod("getVideoController", Null)
   
    If vid.IsInitialized Then
        Dim MediaView As JavaObject
        MediaView.InitializeNewInstance("com/google/android/gms/ads/formats/MediaView".Replace("/", "."), Array(ctxt))
        content.AddView(MediaView, 10dip, 120dip, 200dip, 200dip)
        NativeAdView.RunMethod("setMediaView", Array(MediaView))
    Else
        'If don't has video, check if is there are images
        Dim images As List = NativeAd.RunMethod("getImages", Null)
        If images.IsInitialized And images.Size > 0 Then
            Dim MediaView As JavaObject
            MediaView.InitializeNewInstance("com/google/android/gms/ads/formats/MediaView".Replace("/", "."), Array(ctxt))
            content.AddView(MediaView, 10dip, 120dip, 200dip, 200dip)
            NativeAdView.RunMethod("setImageView", Array(MediaView))
        End If
    End If
     
    '*** 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 = NativeAd.RunMethod("getCallToAction", Null)
    content.AddView(btAction, 10dip, 120dip, 100dip, 50dip)
    NativeAdView.RunMethod("setCallToActionView", Array(btAction))
 
    NativeAdView.RunMethod("setNativeAd", Array(NativeAd))
 
    Activity.AddView(pNativeAdView, 0, 0, 100%x, 200dip)
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);
   }
 
   public void onAdClosed() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adclosed", false, null);
   }
 
   public void onAdFailedToLoad(int arg0) {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_failedtoreceivead", false, new Object[] {String.valueOf(arg0)});
   }
 
   public void onAdLeftApplication() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adleftapplication", false, null);
   }
 
   public void onAdLoaded() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_receivead", false, null);
   }  
 
   public void onAdClicked() {
       processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_clicked", false, null);
   }
}
#End If
Links used to update the code:
Thanks in advance for any help.
 
Last edited:
Top