Android Question [Solved] Setting label sizes based on text ... any solutions #1 Edited

rabbitBUSH

Well-Known Member
Licensed User
Good day in B4XLand,

Please see attached test project zip.

[[ NOTE :
EDIT 1 -> I have replaced the zip file with one generated fro 7-zip. it appears that the tool in the B4XPagesMain does not work with OpenJDK refer here ]]

EDIT 2 -> Sorry ZIPPING has gone wrong will correct it if I can ...... too large . . . working on it.
EDIT 3 -> couldn't get a B4XPages zip file as in first paragraph above - this one is just as first posted - generated from the IDE "File->Extract to Zip" MENU - AT LEAST ONE MEMBER HAS BEEN ABLE TO USE THIS SO .... it is what it is. Let med know if I need to figure out some other way to do this. Thanks for your attention.


I've found two threads that directly deal with the problem of setting a label's size based on the text assigned to it. {And, a bunch of other stuff too - and probably missed some.}

Also, of course, there is this :
lblContent.Height = contenth.MeasureMultilineTextHeight (lblContent, lblContent.Text)​
used in Private Sub CreateItem(....

When the info page is called everything works as expected, except for the above puzzle. The first card and the fifth card show that text that is less-than the height of the label works as expected.

But in all the other cards - the text over-runs the bottom of the label - so - the label has not adjusted size to match the text's length.

The log will show that the label sizes do vary with each assigned text and the panel heights remain constant. It seems - then - that - the label heights are remaining static. This could be a design of the code (which I think originally came from some example - and as below).

I've been through the posts I found (only one is listed in the code - the other is here ) and studied @Peter Simpson 's two useful examples [Peter's examples].

But, this has just foxed me now - (and....I'm getting older).....
 

Attachments

  • testLABEL.zip
    19.5 KB · Views: 162
Last edited:

DonManfred

Expert
Licensed User
Longtime User
B4X Pages Projects MUST be exported with the shortcut in the code (in B4XMainPage). You can not use file-> Export on B4XPages Projects.
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
Export again
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
Export on B4XPages Projects.
MMMMMM .... I got this : in the log

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.swt not found
Completed. Exit code: 1
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
this has just foxed me now
I was able to run your example. You just need to comment this line: #CustomBuildAction: in B4XMainPage
Did you try to add this line:
B4X:
p.SetLayoutAnimated(0, 0, 0, Width,lblContent.Height)
after this line: lblContent.Height = contenth.MeasureMu.....
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
Did you try to add this line:
Yes, I did, thanks, At this point I am not sure there is anything I didn't try. :D Its in the sub CreateItem (.... and - currently (as in the submitted code) is assigning the height from the Dim statement at the sub start . . . .but I did try that as well inserting those calls to utility.SizeToFitWidth( - and - utility.SizeToFitHeight( - but that lable height just stays fixed length/depth.
You just need to comment this line: #CustomBuildAction: in B4XMainPage
Mmmmm that didn't do anything to assist with either zipping the file or when I reran the project to see if the labels thing was corrected....
 
Last edited:
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
Export again
Please see edited #1 for comments on my attempt to get this zipped in the "proper" way - I use OpenJDK and from the link in #1 that appears to be a problem with B4XPages as 32-bit.

Thanks for your earlier comment and help . . .
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
The attached project works as you expect it.
Thanks for the example Klaus - I will study it when our Loadshedding is over - appreciate your assistance.
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
I thought, before marking this solved, to show what the solution provided by @klaus consisted of. And, also, to acknowledge Klaus's assistance.

Lines 12, 20 and 21 below are the additions made by Klaus. Klaus understood that the problem was in the resizing of the PANELS underlying each card since it turned out that the labels were being resized. Line 18 is one I included to create space at the right of labels which allows the text to spread over a wider area per label.

Klaus's additions / solution:
Private Sub CreateItem(Width As Int, Title As String, Image As String, Content As String) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 250dip
    Dim contenth As StringUtils
        
    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 310dip
    
    p.LoadLayout("Card1")
    p.SetLayoutAnimated(0, 0, 0, Width,height)
    p.SetColorAndBorder(Colors.LightGray,0,Colors.LightGray,0)

    pnlCard.Width = Width - 20dip '  <<<<<<<<<<<<<< Klaus's addition

    lblContent.TextSize = 15
    lblTitle.Text   = Title
    lblContent.Text   = Content
    lblContent.Height   = contenth.MeasureMultilineTextHeight (lblContent, lblContent.Text)
    lblContent.Width   = pnlCard.Width - 35dip '  <<<<<<<<<< my dip size <<<< not Klaus's addition

    pnlCard.Height = lblContent.Top + lblContent.Height + 5dip '  <<<<<<<<<<<<<< Klaus's addition
    p.SetLayoutAnimated(0, 0, 0, Width, pnlCard.Height + 10dip) '  <<<<<<<<<<<<<< Klaus's addition

    Return p
End Sub
 
Upvote 0
Top