Android Question [Solved] Native Ads Advanced with MediaView

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code creates a MediaView, adds it to the content panel and calls setMediaView:
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))
 
Upvote 0

asales

Expert
Licensed User
Longtime User
The id of the Ad unit to test the native advanced with mediaview is:
ca-app-pub-3940256099942544/1044960115
(https://developers.google.com/admob/android/test-ads)

I changed the code to test if ad has a video:
B4X:
Dim vid As JavaObject = NativeUnifiedAd.RunMethod("getVideoController", Null)
Dim hasvid As Boolean = vid.RunMethod("hasVideoContent", Null)
If vid.IsInitialized 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("setMediaView", Array(MediaView))
End If
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Updated code:

Checks if there is a video, if don't check for images.

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, 10dip, 40dip, 200dip, 200dip)
       NativeUnifiedAdView.RunMethod("setMediaView", Array(MediaView))
    End If
Else
    Dim images As List = NativeUnifiedAd.RunMethod("getImages", Null)
    If images.IsInitialized And images.Size > 0 Then
        Dim imgView As Panel = pAdmob.GetView(5)  'example
        Dim image As JavaObject = images.Get(0)
        imgView.Background = image.RunMethod("getDrawable", Null)
        NativeUnifiedAdView.RunMethod("setImageView", Array(imgView))
    End If
End If
 
Upvote 0
Top