Android Question [SOLVED]Canvas Problem

kohlenbach

Member
Licensed User
Hi,
I draw a retangle and in the retangle a Text. I convert the panel to a view to change the border color and radius.
Why? I want rounded corners and I need to change the border color when I click on the panel.

This works fine in b4i, but in b4a the text from cvs.DrawText is gone.

What can I do that it works like in b4i ?

B4X:
Dim cvs As B4XCanvas
    Dim p As Panel
    p.Initialize("test")
    p.Width=50
    p.Height=50

    Dim color As Int = xui.color_red

    cvs.Initialize(p)
    cvs.Resize(p.Width, p.Height)

    Dim rect As B4XRect
    rect.Initialize(0, 0, 50, 50)


    cvs.DrawRect(rect,color, True, 1dip)

    Dim fontsize As Int =14
    cvs.DrawText("Test",p.Width/2,(p.Height/2)+(fontsize/2),xu.CreateDefaultFont(fontsize),xui.Color_Black,"CENTER")
   
    'In b4a my drawtext is gone after setcolorandborder, in b4i it works fine
    Dim view As B4XView
    view = p
    view.SetColorAndBorder(color,1dip,color,5)
 

PaulMeuris

Well-Known Member
Licensed User
There was an error in your code: xu.CreateDefaultFont should be xui.CreateDefaultFont.
The issue here is that the view will be set to ColorDrawable in B4A.
You can solve this by redrawing the text (see draw_text subroutine) after you change the color and border.

1760755631641.png

1760755411573.png
1760755441898.png

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public p As Panel
    Public cvs As B4XCanvas
    Public color As Int
End Sub
Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    p.Initialize("test")
    p.Width=250dip
    p.Height=250dip
    color = xui.Color_Red
    p.As(B4XView).SetColorAndBorder(xui.Color_Transparent,5dip,color,10dip)
    cvs.Initialize(p)
    cvs.Resize(p.Width, p.Height)
    p = draw_text(cvs,p,color,"Testing red")
    Root.AddView(p,10dip,10dip,p.Width,p.Height)
End Sub
Private Sub draw_text(cnvs As B4XCanvas,pnl As Panel,clr As Int,text As String) As Panel
    Dim rect As B4XRect
    rect.Initialize(0dip, 0dip, 250dip, 250dip)
    cnvs.DrawRect(rect,clr, False, 1dip)
    Dim fontsize As Int = 20
    cnvs.DrawText(text,(pnl.Width/2),(pnl.Height/2),xui.CreateDefaultFont(fontsize),xui.Color_Black,"CENTER")
    Return pnl
End Sub
Private Sub test_Click
    If color = xui.Color_Red Then
        color = xui.Color_Green
    Else
        color = xui.Color_Red
    End If
    p.As(B4XView).SetColorAndBorder(xui.Color_Transparent,5dip,color,10dip)
    cvs.Initialize(p)
    If color = xui.Color_Red Then
        p = draw_text(cvs,p,color,"Testing red")
    Else
        p = draw_text(cvs,p,color,"Testing green")
    End If
End Sub
 
Upvote 0

kohlenbach

Member
Licensed User
There was an error in your code: xu.CreateDefaultFont should be xui.CreateDefaultFont.
The issue here is that the view will be set to ColorDrawable in B4A.
You can solve this by redrawing the text (see draw_text subroutine) after you change the color and border.

View attachment 167905
View attachment 167903 View attachment 167904
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public p As Panel
    Public cvs As B4XCanvas
    Public color As Int
End Sub
Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    p.Initialize("test")
    p.Width=250dip
    p.Height=250dip
    color = xui.Color_Red
    p.As(B4XView).SetColorAndBorder(xui.Color_Transparent,5dip,color,10dip)
    cvs.Initialize(p)
    cvs.Resize(p.Width, p.Height)
    p = draw_text(cvs,p,color,"Testing red")
    Root.AddView(p,10dip,10dip,p.Width,p.Height)
End Sub
Private Sub draw_text(cnvs As B4XCanvas,pnl As Panel,clr As Int,text As String) As Panel
    Dim rect As B4XRect
    rect.Initialize(0dip, 0dip, 250dip, 250dip)
    cnvs.DrawRect(rect,clr, False, 1dip)
    Dim fontsize As Int = 20
    cnvs.DrawText(text,(pnl.Width/2),(pnl.Height/2),xui.CreateDefaultFont(fontsize),xui.Color_Black,"CENTER")
    Return pnl
End Sub
Private Sub test_Click
    If color = xui.Color_Red Then
        color = xui.Color_Green
    Else
        color = xui.Color_Red
    End If
    p.As(B4XView).SetColorAndBorder(xui.Color_Transparent,5dip,color,10dip)
    cvs.Initialize(p)
    If color = xui.Color_Red Then
        p = draw_text(cvs,p,color,"Testing red")
    Else
        p = draw_text(cvs,p,color,"Testing green")
    End If
End Sub



Hi, thanks, the error was from typing. But your example has for me one problem. I have not the text in the click event,
I only want to change the border, but I dont have the text to write again.
How I say, I can change the border in b4i without loosing the text or draw again the text.
I have a crossplatform project b4i and b4a, and its a pitty the things have the same "name" but
have different behaviors.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You can set several canvas and stack them... then just redraw the concerned one
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If I remember correctly, the canvas 'draws' on the views background, so No, no option but to redraw the whole canvas. If it is that important, you could stack two views.
 
Upvote 0
Top