Android Question [Solved] Set BitmapDrawable gravity = Center (Native Ads Advanced)

asales

Well-Known Member
Licensed User
I use this code to set a image in native ad advanced.
B4X:
Dim images As List = NativeContentAd.RunMethod("getImages", Null)
If images.IsInitialized And images.Size > 0 Then
    Dim image As JavaObject = images.Get(0)
    logoView.Background = image.RunMethod("getDrawable", Null)
    NativeContentAdView.RunMethod("setImageView", Array(logoView))
End If
The logoView is a panel and the background is set to FILL automatically. This is the result.:
native_ad1.jpg


How I can set the BitmapDrawable with gravity = CENTER ?

I try this:
B4X:
logoView.SetBackgroundImage(image.RunMethod("getDrawable", Null)).Gravity = Gravity.CENTER
but I get this error:
B4X:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.Bitmap

Thanks in advance for any tip.
 

User242424

Member
Licensed User
I try this:
B4X:
logoView.SetBackgroundImage(image.RunMethod("getDrawable", Null)).Gravity = Gravity.CENTER

Gravity exists on ImageView methods. so this should work :
B4X:
Dim images As List = NativeContentAd.RunMethod("getImages", Null)
If images.IsInitialized And images.Size > 0 Then
    Dim image As JavaObject = images.Get(0)
    logoView.Background = image.RunMethod("getDrawable", Null)
    logoView.Gravity = Gravity.CENTER
    NativeContentAdView.RunMethod("setImageView", Array(logoView))
End If
 
Upvote 0

asales

Well-Known Member
Licensed User
I found a workaround, using this code snippet.
I don't know If is the best approach, but solved the problem.
B4X:
Dim logoView As B4XView = pAdmob2.GetView(5)
Dim image As JavaObject = images.Get(0)
                
Dim bd As BitmapDrawable
bd = image.RunMethod("getDrawable", Null)
FitImageToView(bd.Bitmap, logoView)
native_ad2.jpg
 
Upvote 0
Top