Android Question Accessing the Bitmap Drawable of a Custom View

Alessandro71

Well-Known Member
Licensed User
Longtime User
I'm creating a B4X Custom View
I want to set the background of a label to the BitmapDrawable set in the properties of the Base Background in the Designer

The following code results in an empty background for the label, while other properties are correctly passed (like Text)
B4X:
'Base type must be Object
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    
    Lbl_button.Initialize("")
    Lbl_button.Background = Lbl.Background
...
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
What is the output of Log(Lbl.Background) ?

null

Reusing drawables doesn't always work. It is better to check if it is BitmapDrawable, cast it and create a new BitmapDrawable for the button.

i also added this code
B4X:
    If Lbl.Background Is BitmapDrawable Then
        Log("is bitmap drawable")
    Else
        Log("is NOT bitmap drawable")
    End If
and it asserts it's NOT a bitmap drawable

this is how is see it in the designer
common properties list position and size are correctly accounted


1577639501063.png
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
but there's no mBase.Background property
how do I get the base background settings to create a new BitmapDrawable to set my new label?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You can always go from B4XView to the "native" view and vice versa.
B4X:
Dim pnl As Panel = mBase
Log(pnl.Background)

I was already trying (I am slow :D:(:(:()

B4X:
Public Sub getBaseDrawable As Object
    Dim BaseDrawable As Object
    #If B4A
        Dim pnl As Panel = mBase
        BaseDrawable = pnl.Background
    #Else If B4J
        Dim pn As Pane = mBase
        BaseDrawable = pn.Snapshot
    #End If
    Return BaseDrawable
End Sub

Since your trying to create a B4X CV, it should work not only with B4A but at least even with B4J and/or B4i.

1 - I don't know-have B4i
2 - Pane (B4J) does not provide its drawable, then in the code above the property will return a B4J Image.
 
Last edited:
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
as a followup, it worked perfectly
I just had to remove the BitmapDrawable from the base, or it will be drawn twice

B4X:
        'remove the base background
        basepanel.Color = xui.Color_Transparent
 
Upvote 0
Top