Android Question Native Ads Advanced with MediaView Not Working

Discussion in 'Android Questions' started by Lucas Eduardo, Sep 18, 2018.

  1. Lucas Eduardo

    Lucas Eduardo 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:
    Code:
    Dim MediaView As JavaObject
    MediaView.InitializeNewInstance(
    "com/google/android/gms/ads/formats/MediaView".Replace("/""."), Array(ctxt))
    content.AddView(MediaView, 
    00100dip100dip)
    NativeContentAdView.RunMethod(
    "setMediaView"Array(MediaView))
    my sub:
    Code:
    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, 
    0dip0dip100dip100dip)
      
        pNativeAdView.AddView(content, 
    00100%x300dip)
      
      
        
    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, 
    0dip10dip350dip40dip)
        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, 
    10dip50dip330dip80dip)
        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, 
    10dip132dip80dip80dip)
            NativeContentAdView.RunMethod(
    "setIconView"Array(logoView))
        
    Else
            
    Dim logoViewImg As ImageView
            logoViewImg.Initialize(
    "")
            content.AddView(logoViewImg, 
    10dip132dip80dip80dip)
            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, 
    130dip132dip80dip80dip)
            NativeContentAdView.RunMethod(
    "setImageView"Array(imgView))
        
    Else
            
    Dim imgViewImg As ImageView
            imgViewImg.Initialize(
    "")
            content.AddView(imgViewImg, 
    130dip132dip80dip80dip)
            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(10415956),3dip)
        btAction.Background = cd
        btAction.TextColor = 
    Colors.White
        btAction.Text = NativeContentAd.RunMethod(
    "getCallToAction"Null)
        content.AddView(btAction, 
    240dip150dip100dip50dip)
        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
     
  2. asales

    asales Well-Known Member Licensed 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:
    Code:
    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, 
    20dip20dip220dip110dip)
                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
     
  3. Lucas Eduardo

    Lucas Eduardo 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
     
  4. asales

    asales Well-Known Member Licensed User

    I don't know how to show image in MediaView.
    Maybe @Erel can give us an example.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. asales

    asales Well-Known Member Licensed User

    I think I got it:
    Code:
    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, 
    10dip40dip200dip200dip)
            NativeUnifiedAdView.RunMethod(
    "setImageView"Array(MediaView))
       
    End If
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice