Android Question Native Ads - MediaView size problem

Discussion in 'Android Questions' started by Jack Cole, Oct 21, 2018.

  1. Jack Cole

    Jack Cole Active Member Licensed User

    I inserted the following code into the example code for Native Ads as originally posted by Erel here.

    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))
    The MediaView turns out to be tiny. I tried changing the length and width to 300dip, but it is still way too small compared to the dimensions it should be. Any ideas?

    upload_2018-10-21_12-47-36.png
     
  2. Jack Cole

    Jack Cole Active Member Licensed User

    I think I have answered my own question. The MediaView seems to try to fit the image the best that it can based on the other content in the ad.
     
  3. Jack Cole

    Jack Cole Active Member Licensed User

    This continues to be a struggle. The size seems to be something like 1/2 of the actual dimensions. Anyone else using the mediaview?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. Jack Cole

    Jack Cole Active Member Licensed User

    Hi Erel,

    I'm not sure that is the same problem. It is more like the view is always set too small. But maybe that code would help resolve it? I'm including some integrated code below and show what it looks like compared to the original. I overlay the MediaView on the original ImageView so you can see how big it is in comparison. The fact that the MediaView image is centered vertically, makes me think the view itself may be the right size. For whatever reason, it's not scaling correctly.

    upload_2018-11-5_5-43-6.png

    Code:
    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)))
        
    Log("trying to load ad")
        
    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, 
    00300dip300dip)
        
    Dim lbl As Label
        lbl.Initialize(
    "")
        lbl.Text = NativeContentAd.RunMethod(
    "getHeadline"Null)
        content.AddView(lbl, 
    10dip10dip300dip50dip)
        NativeContentAdView.RunMethod(
    "setHeadlineView"Array(lbl))
        
    Dim lbl2 As Label
        lbl2.Initialize(
    "")
        lbl2.Text = NativeContentAd.RunMethod(
    "getBody"Null)
        content.AddView(lbl2, 
    10dip60dip300dip50dip)
        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, 
    200dip0100dip100dip)
            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, 
    200dip100dip100dip100dip)
            NativeContentAdView.RunMethod(
    "setImageView"Array(imgView))
        
    End If
    '    Dim imgView As Panel
    '    imgView.Initialize("")
    '    content.AddView(imgView, 100dip, 100dip, 100dip, 100dip)
        'New mediaview
        Dim MediaView As JavaObject
        MediaView.InitializeNewInstance(
    "com/google/android/gms/ads/formats/MediaView".Replace("/""."), Array(ctxt))
        content.AddView(MediaView, imgView.Left, imgView.Top, imgView.Width, imgView.Height)
    '    imgView.RemoveView
        NativeContentAdView.RunMethod("setMediaView"Array(MediaView))
        
    'end new mediaview code
        
        NativeContentAdView.RunMethod(
    "setNativeAd"Array(NativeContentAd))
        
    Activity.AddView(pNativeAdView, 00100%x200dip)
    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
     

    Attached Files:

  6. Jack Cole

    Jack Cole Active Member Licensed User

    Was thinking more about what that layout looked like. I doubled the with of the MediaView, and that makes it fit pretty close. I changed the following code.

    Code:
    content.AddView(MediaView, imgView.Left, imgView.Top, imgView.Width*2, imgView.Height)
    Here is the result with the MediaView overlaying the original ImageView. I'm ok with using this code, but I don't understand what the problem is, which makes me a little nervous to use it in production.

    upload_2018-11-5_5-53-18.png
     
  7. asales

    asales Well-Known Member Licensed User

    A tip:
    See this Release Notes:
    https://developers.google.com/admob/android/rel-notes

    " Native ads: NativeAppInstallAd, NativeContentAd, and associated APIs are deprecated in favor of UnifiedNativeAd. Refer to Native Ads Advanced Unified for further instructions on using the new APIs. "
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You can find the bitmap size with this code:
    Code:
    If imgView.Background Is BitmapDrawable Then
       
    Dim bd As BitmapDrawable = imgView.Background
       
    Dim bmp As Bitmap = bd.Bitmap
       
    Log(bmp)
    End If
    The dimensions I used in my example can and most probably should be changed.
     
    Johan Hormaza and asales like this.
  9. Jack Cole

    Jack Cole Active Member Licensed User

    Hi Erel,
    I guess I'm not sure how measuring the bitmap will help. Let me state the problem another way. The MediaView has to be set to twice the width of the previously-used ImageView in order to render an image that is the same size as the ImageView. Is this what you would expect?
    Thanks,
    Jack
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    I think that the source of the confusion is the assumption that the dimensions in my original example are strict. I've just used them based on a few tests.
     
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