B4J Question A challenge for the Math Wizards...

Cableguy

Expert
Licensed User
Longtime User
Hi guys...

I need to create a layout that consists of a Pane (30px Height, FullScreen Width using anchors), A Label (30px by 30px, Anchored Left), A Label (30px by 30px, Anchored Right)...
Both labels will be used as Left/righr Arrows, and the space in between them is to be populated with n Labels, with minimum 100px Width...
I need a way (formula) to automatically resize these "in between" labels in a way that no label is less than 100px Width and no more than 150px Width BUT that only entire Labels occupy the "in between" space.

So, if only 4 labels can be shown and the space in between is of 440, each label will be 110px, but If the Space is like 1240, show as many as possible, Allowing also a small repositioning of the Arrows to occupy some unused spaced...

I hope I was clear enough... needless to say I have no math skills!!!
 

barx

Well-Known Member
Licensed User
Longtime User
I think I would create a layout that adds the arrows etc, but just add a panel as a placeholder for the labels.

Then in code do something like this:

Get the usable width, calculate the possible number of labels.

Find out how many you can fit by doing

B4X:
Dim LabelCount as Int

LabelCount = usableWidth \ 100 'does b4j even support integer divide ;)
Then work out max width

B4X:
Dim LabelWidth as Int

LabelWidth = usableWidth / LabelCount

Then create the label in a code loop 'LabelCount' amount of times and set the labels width to
B4X:
Min(LabelWidth, 150px)

p.s. I have no idea if b4x supports integer divide '\'. If not 'Floor' may be the alternative
pps none of the code is tested and probably wont wrk but you should get the idea ;)
ppps I normally get the answer to questions wrong so don't waste too much time on it if it doesn't work, lol
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Sorry, but the question is not complete.
Is the number of Labels constant, or do you want to add the Labels dynamically?
Why not use a ScrollPane between the two arrow labels?

I will have about 20 or 25 labels (fixed quantity), so the total Width of all the labels will be larger than the screen, hence the Arrows at both edges...
Basically they will act as Tabs, like in a tab-pane, but without the panes.

taking @barx idea I can take the available width and divide by 100, and see if the rest is divisible by the quantity result from the first division...
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Paulo,
Attached a small demo program.
Sorry for beeing a bit late but I was quite busy the last days.
And it didn't work as I expected it to work, look HERE, but now it's OK.
 

Attachments

  • TestLayout.zip
    2.6 KB · Views: 243
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Klaus, I tested your code and it is almost perfect...

But, In the meantime, I started a CustomView Of my own, and its current state has pasted the "can make changes without needing to re-start the project" State.
I've managed to place the labels according to either a minimum Width or TabCount set by user (I will convert it into a Library).
So it the user set the tab Width to be, say 150px, it will calculate how many 150px Wide labels it can accommodate within the available space, and then stretch them to occupy the left over space. Same calculations go if the user sets a Tab count value, say 3 Tabs, it will calculate the Max Width and re adjust the arrows (within 1 to 5 px in each side).
 
Upvote 0
Top