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.

    Dim MediaView As JavaObject
    "com/google/android/gms/ads/formats/MediaView".Replace("/""."), Array(ctxt))
    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?

  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.


    Sub LoadNativeAd
    Dim AdUnitId As String = "ca-app-pub-3940256099942544/2247696110"
    Dim ctxt As JavaObject
    Dim builder As JavaObject
    ""Array(ctxt, AdUnitId))
    Dim OnContentAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeContentAd.OnContentAdLoadedListener".Replace("/""."), _
    Dim Listener As JavaObject
    Application.PackageName & ".main$MyAdListener"Array("NativeAd"))  'change 'main' with the current activity module name
    Dim AdLoader As JavaObject = builder.RunMethod("build"Null)
    Dim AdRequestBuilder As JavaObject
    "com/google/android/gms/ads/AdRequest.Builder".Replace("/""."), Null)
    Log("trying to load ad")
    Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)
    Dim NativeContentAd As JavaObject = Args(0)
    Dim NativeContentAdView As JavaObject
    "com/google/android/gms/ads/formats/NativeContentAdView".Replace("/""."), _
    Dim pNativeAdView As Panel = NativeContentAdView
    Dim content As Panel
    Dim lbl As Label
        lbl.Text = NativeContentAd.RunMethod(
    Dim lbl2 As Label
        lbl2.Text = NativeContentAd.RunMethod(

    Dim logo As JavaObject = NativeContentAd.RunMethod("getLogo"Null)
    If logo.IsInitialized Then
    Log("adding logo")
    Dim logoView As Panel
            logoView.Background = logo.RunMethod(
    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
    Dim image As JavaObject = images.Get(0)
            imgView.Background = 
    End If
    '    Dim imgView As Panel
    '    imgView.Initialize("")
    '    content.AddView(imgView, 100dip, 100dip, 100dip, 100dip)
        'New mediaview
        Dim MediaView As JavaObject
    "com/google/android/gms/ads/formats/MediaView".Replace("/""."), Array(ctxt))
        content.AddView(MediaView, imgView.Left, imgView.Top, imgView.Width, imgView.Height)
    '    imgView.RemoveView
    'end new mediaview code
    Activity.AddView(pNativeAdView, 00100%x200dip)
    End Sub
    Sub NativeAd_FailedToReceiveAd (ErrorCode As String)
    Log("NativeAd_FailedToReceiveAd: " & ErrorCode)
    End Sub

    Sub NativeAd_AdOpened
    End Sub

    #if Java
    public static class MyAdListener extends {
       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 onAdOpened() {
           processBA.raiseEventFromDifferentThread(null, null, 0, eventName + "_adopened", false, null);
       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.

    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.

  7. asales

    asales Well-Known Member Licensed User

    A tip:
    See this Release 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:
    If imgView.Background Is BitmapDrawable Then
    Dim bd As BitmapDrawable = imgView.Background
    Dim bmp As Bitmap = bd.Bitmap
    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?
  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.
  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