Android Question placing views in a grid

Cableguy

Expert
Licensed User
Hi GURUS!

I'm adding 140 square labels to a panel using this

B4X:
Private i As Int = 0
    For x = 0 To 9 'Rows
        For y = 0 To 13 'collumns
            ColorBox(i).Initialize("ColorSelected")
            ColorBox(i).Tag = Palette.get(i)
            mBase.AddView(ColorBox(i), Floor(mBase.Width/30 + ((mBase.Width/15)*y)),Floor(mBase.Height/15 + ((mBase.Height/15)*x)), Floor(mBase.Width/14),Floor(mBase.Height/15))
            ColorBox(i).Color = Colors.Red
            i=i+1
        Next
    Next
the labels are correctly placed BUT I get a few horizontal lines with the background color, meaning that the bottom on the row does not coincide with the top of the next row, so instead of a "solid" red 14x10 square, I'm getting 4 or 5 odd rectangles. Vertical spacing seems to be right thou.
I need help with my placing formula
 

klaus

Expert
Licensed User
Why do you use one time mBase.Width/15 and another one mBase.Width/14 ?

I would do it that way:
B4X:
    Private i As Int = 0
    Private row, col As Int
    Private x0 = mBase.Width/30 As Int
    Private w = x0 * 2 As Int
    Private y0 = mBase.Height / 30 As Int
    Private h = y0 * 2 As Int
    For row = 0 To 9 'Rows
        For col = 0 To 13 'collumns
            ColorBox(i).Initialize("ColorSelected")
            ColorBox(i).Tag = Palette.get(i)
            mBase.AddView(ColorBox(i), x0 + w * col, y0 + h * row, w, h)
            ColorBox(i).Color = Colors.Red
            i=i+1
        Next
    Next
You must use integer variables for the dimensions.
 

Cableguy

Expert
Licensed User
the user will be able to set the width/height of this view and this way I maintain the views occupation ratio
Thanks for the example code, really easy to read this way
 
Top