Android Question Native Ads Advanced with MediaView Not Working

Lucas Eduardo

Active Member
Licensed User
This is my sub with Media View implemented and not working, how can i implement the Erel's mediaview code in this sub?
Erel's code:
B4X:
Dim MediaView As JavaObject
MediaView.InitializeNewInstance("com/google/android/gms/ads/formats/MediaView".Replace("/", "."), Array(ctxt))
content.AddView(MediaView, 0, 0, 100dip, 100dip)
NativeContentAdView.RunMethod("setMediaView", Array(MediaView))

my sub:
B4X:
Sub LoadNativeAd(pane As Panel,left As Int, top As Int, Width As Int, Height As Int)
    Dim ctxt As JavaObject
    ctxt.InitializeContext
  
    Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdNativoAdvanced))
  
    '*** 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 & ".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)))
  
    '*** 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
  
    Dim content As Panel
    content.Initialize("")
  
    Dim MediaView As JavaObject
    MediaView.InitializeNewInstance("com/google/android/gms/ads/formats/MediaView".Replace("/", "."), Array(ctxt))
    content.AddView(MediaView, 0dip, 0dip, 100dip, 100dip)
  
    pNativeAdView.AddView(content, 0, 0, 100%x, 300dip)
  
  
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Typeface = Typeface.DEFAULT_BOLD
    lbl.TextSize = 15
    lbl.Gravity = Gravity.CENTER
    lbl.TextColor = Colors.DarkGray
    lbl.Text = NativeContentAd.RunMethod("getHeadline", Null)
    content.AddView(lbl, 0dip, 10dip, 350dip, 40dip)
    NativeContentAdView.RunMethod("setHeadlineView", Array(lbl))
  
  
    Dim lbl2 As Label
    lbl2.Initialize("")
    lbl2.TextSize = 13
    lbl2.TextColor = Colors.DarkGray
    lbl2.Text = NativeContentAd.RunMethod("getBody", Null)
    lbl2.Gravity = Gravity.TOP
    Log(lbl2.Text)
    content.AddView(lbl2, 10dip, 50dip, 330dip, 80dip)
    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, 10dip, 132dip, 80dip, 80dip)
        NativeContentAdView.RunMethod("setIconView", Array(logoView))
    Else
        Dim logoViewImg As ImageView
        logoViewImg.Initialize("")
        content.AddView(logoViewImg, 10dip, 132dip, 80dip, 80dip)
        logoViewImg.Bitmap = LoadBitmapResize(File.DirAssets,"gift.png",logoViewImg.Width,logoViewImg.Height,True)
    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, 132dip, 80dip, 80dip)
        NativeContentAdView.RunMethod("setImageView", Array(imgView))
    Else
        Dim imgViewImg As ImageView
        imgViewImg.Initialize("")
        content.AddView(imgViewImg, 130dip, 132dip, 80dip, 80dip)
        imgViewImg.Bitmap = LoadBitmapResize(File.DirAssets,"gift.png",imgViewImg.Width,imgViewImg.Height,True)
    End If
  
    '*** Code changed: new button to action
    Dim btAction As Button
    btAction.Initialize("")
    Dim cd As ColorDrawable
    cd.Initialize(Colors.RGB(104, 159, 56),3dip)
    btAction.Background = cd
    btAction.TextColor = Colors.White
    btAction.Text = NativeContentAd.RunMethod("getCallToAction", Null)
    content.AddView(btAction, 240dip, 150dip, 100dip, 50dip)
    NativeContentAdView.RunMethod("setCallToActionView", Array(btAction))
  
   Log("seta AD")
    'NativeContentAdView.RunMethod("setNativeAd", Array(NativeContentAd))
  
  
    NativeContentAdView.RunMethod("setMediaView", Array(MediaView))
  
    pane.AddView(pNativeAdView,left,top,Width,Height)
End Sub
 

asales

Expert
Licensed User
Longtime User
The unit ID to Native Advanced Video example is:
ca-app-pub-3940256099942544/1044960115

Check if there is a video in the ad, if don't you show the image.

Here:
B4X:
Dim vid As JavaObject = NativeUnifiedAd.RunMethod("getVideoController", Null)
Dim hasvid As Boolean = vid.RunMethod("hasVideoContent", Null)
If hasvid = True Then
        If vid.IsInitialized Then
            Dim MediaView As JavaObject
            MediaView.InitializeNewInstance("com/google/android/gms/ads/formats/MediaView".Replace("/", "."), Array(ctxt))
            content.AddView(MediaView, 20dip, 20dip, 220dip, 110dip)
            NativeUnifiedAdView.RunMethod("setMediaView", Array(MediaView))
        End If
    Else
        'If don't has video, check if is there images
        Dim images As List = NativeContentAd.RunMethod("getImages", Null)
        (...)
End If
 
Upvote 0

Lucas Eduardo

Active Member
Licensed User
But in the Admob notification said that we have to have a mediaview when the ad has a video and in the main images as well. And in your code you're only putting videos
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I think I got it:
B4X:
    Else   
   'If don't has video, check if is there images
   Dim images As List = NativeUnifiedAd.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, 40dip, 200dip, 200dip)
        NativeUnifiedAdView.RunMethod("setImageView", Array(MediaView))
   End If
 
Upvote 0
Top