Android Question Native Ads - MediaView size problem

Jack Cole

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

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))
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
 

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


B4X:
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, 0, 0, 300dip, 300dip)
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = NativeContentAd.RunMethod("getHeadline", Null)
    content.AddView(lbl, 10dip, 10dip, 300dip, 50dip)
    NativeContentAdView.RunMethod("setHeadlineView", Array(lbl))
    Dim lbl2 As Label
    lbl2.Initialize("")
    lbl2.Text = NativeContentAd.RunMethod("getBody", Null)
    content.AddView(lbl2, 10dip, 60dip, 300dip, 50dip)
    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, 200dip, 0, 100dip, 100dip)
        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, 200dip, 100dip, 100dip, 100dip)
        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, 0, 0, 100%x, 200dip)
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
 

Attachments

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.

B4X:
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
 

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
 
Top