Android Question Understanding the panel and canvas

kpmais

Member
Licensed User
Longtime User
I am a little surprised at the strange behavior of canvas and panel. When I create a panel, give it a color and create a canvas (no color, just the canvas) the color of the panel disappears. Does a canvas have a basic color, white? If I assign a color to the panel after creating the canvas, it will appear. I don't understand the behavior. If I only create the canvas, why does this override the panel color?

example:


B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim tpanel As Panel
    Dim tcanvas As Canvas
    Dim trect As Rect
    
    
    tpanel.Initialize("tpanel")
    tpanel.Color=Colors.Blue
    Activity.AddView(tpanel,0,0,Activity.Width,Activity.Height)
    tpanel.Color=Colors.Blue
    tcanvas.Initialize(tpanel)
    tpanel.Color=Colors.Blue ' without this line, the panel+canvas remains colorless, why?
'    trect.Initialize(0,0,Activity.Width,Activity.Height)
'    tcanvas.DrawRect(trect,Colors.Transparent,True,0)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    
End Sub
 

kpmais

Member
Licensed User
Longtime User
After trying it all over again, I realize I must have made a mistake. The panel shows the right color. User error.
But if I provide the canvas with a transparent rectangle, I do not understand that the panel is losing its color.
To test the code, just activate the commented out lines.
Why is the underlying color being overwritten by a transparent rectangle?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In B4A and B4i, when you draw with a Canvas and transparent color, the background of the Panel is set to transparent and the color of the underlying view is shown.
In B4J, drawing with a transparent color does nothing, only ClearRect sets the background to transparent.
 
Upvote 0
Top