Bug? Canvas measurestringwidth problem


Hello everyone. I have a small problem using canvas.
I have a code that i want to create many labels in two panels that they belong to 2 scrollviews.
idiotLayout - (Auto Poilitis) Visual Designer 28_04_2023 15_42_05.png

In the fPanel (scrollview1) and sPanel (scrollview2) I create the labels in line, one next to another until it reaches the end of the screen. When that happens it goes in the next row and continues.
I can calculate the width of every panel using a canvas to measure its text. So I create a background of every label as much as its text is. That works perfectly.

Here it does it:
If beforebtnslength > Activity.Width Then   
    ycounter = ycounter + 1
    beforebtnslength = 0
Else If beforebtnslength + SQLReader.GetString("name").Length*3%x > Activity.Width Then
    ycounter = ycounter + 1
    beforebtnslength = 0
    xcounter = xcounter + 1
End If

'cPanel is fpanel or spanel, depends on where I want to write'
'btn is a label'
cPanel.AddView(btn, beforebtnslength, ycounter * (50dip), 1%x ,2%y)

'cvsTest is canvas'

btn.Height = cvsTest.MeasureStringHeight(btnText, Typeface.DEFAULT, 40)
btn.Width = cvsTest.MeasureStringWidth(btnText, Typeface.DEFAULT, 30) + 10dip

beforebtnslength = btn.Width + beforebtnslength + 10dip

The problem is that in the last label it creates in sPanel, the label that has been created has not the right width. Actually the last label that is been created it the sPanel has the width of the last label that was created in fPanel!
The funny is that all other label that there are in fPanel or sPanel have the right width, their text width.

Does anyone know why it happens?

Note: If I run my code using breakpoints the last label has the right width, its text width. Otherwise, it keeps the last label width of the fPanel.


Thanks for the answer Erel!
My whole code is about 1000 lines. I tried to write only the part I create the labels.
What do you mean the layout anchors are wrong? I create dynamic the labels using addview in the panel that you can see from the designer. Do you mean I have to change the anchors of the panels from the designer?
Also what do you mean that I do not use B4XPages?