B4J Question TextArea content height

rbirago

Active Member
Licensed User
Longtime User
How to know which has to be the right height of a TextArea to show completely his content? I know that for example in a BBCodeView I can know his content height using BBCodeView.sv.ScrollViewContentHeight, but what about TextArea? I have tried to redefine the TextArea to a B4XView, but the method ScrollViewContentHeight is not suitable for it (actually TextArea is a kind of ScrollView...).
Thanks in advance
Roberto
 

Daestrum

Expert
Licensed User
Longtime User
Using javaobject/reflection you can use 'setPrefRowCount' to give you the height in lines of text.
B4X:
    Dim tjo As JavaObject = Text_Area
    tjo.RunMethod("setPrefRowCount",Array(10))   ' textarea will be 10 lines high
 
Upvote 0

rbirago

Active Member
Licensed User
Longtime User
Really my problem is the opposite: I want to know which is the virtual height of the content of the TextArea and so eventually I can change the size of the whole TextArea View to show the entire text inside.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
I have had success with this method , but still needs some thought. Code courtesy of @stevel05

B4X:
Type TextMetric(Width As Double, Height As Double)
Private TextArea1 As TextArea     ' or B4XView

Dim myString As String = $"Lorem Ipsum is simply dummy text of the printing and typesetting industry.
     Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
     printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,
     but also the leap into electronic typesetting, remaining essentially unchanged."$

    Dim TM As TextMetric = MeasureText(myString,fx.DefaultFont(16))  'same font size set in designer

    Sleep(0)  'This solved issue with other view height changes as well.

   'TextArea1.SetLayoutAnimated(0, TextArea1.Left, TextArea1.Top , TextArea1.Width, (TM.Width / TextArea1.Width) * (TM.Height *  1.5))  'As B4XView
    TextArea1.PrefHeight = (TM.Width/TextArea1.Width) * (TM.Height *  1.5)   'As Text Area  ( 1.5 ?? )
    TextArea1.Text = myString


Sub MeasureText(Text As String,TFont As Font) As TextMetric
    Dim TM As TextMetric
    TM.Initialize
    Dim T As JavaObject
    T.InitializeNewInstance("javafx.scene.text.Text",Array(Text))
    T.RunMethod("setFont",Array(TFont))
    TM.Width = T.RunMethod("prefWidth",Array(-1.0))
    TM.Height = T.RunMethod("prefHeight",Array(TM.Width))
    Return TM
End Sub


This is another possible solution ...
 
Last edited:
Upvote 0

rbirago

Active Member
Licensed User
Longtime User
Thanks for suggestion. I am thinking that it could be another conceptual way: when you put in a TextArea a text longer than the view can display, the TextArea view create a scrollbar. This means that the view KNOWS the virtual height to show the entire text. So shurely exists in the view a property of the virtual height to show the entire text. The question is: is this property available? Which is the name of this property?
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Why not just measure the Text before you populate the TextArea View .. ?
Then adjust the TextArea view dimensions to suit , then add the Text / String.
 
Upvote 0

rbirago

Active Member
Licensed User
Longtime User
yes, I understand that the way you suggest can work, but I think that to use the built-in capability of TextArea to understand the virtual height (it uses this calculation to set and size its scrollbar) should be a more linear (and automatized) method. I have Googled about TextArea fx properties, but for now no answers...
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this and see if it gives you what you want:
B4X:
Sleep(0)
Dim TAJO as JavaObject = TextArea1
Dim Content As JavaObject = TAJO.RunMethod("lookup",Array(".content"))
If Content.IsInitialized Then
    'Get the layoutbounds property
    Dim LayoutBoundsProperty As JavaObject = Content.RunMethod("getLayoutBounds",Null)
    Log(LayoutBoundsProperty.RunMethod("getMaxY",Null))
End If

Depends on the JavaObject library

The layout will need to be completely finished before you can run this code as it will not find the Content pane otherwise.

Minimum height returned is the inner height of the laid out view.

If you want to be notified when the height changes, see the code in my TextArea lined customview and ask if it's not clear.
 
Last edited:
Upvote 0

rbirago

Active Member
Licensed User
Longtime User
I have tried getPrefHeight, but it gives me the same value of Height. Perhaps the solution posted #4 is the only way...or eventually to substitute TextArea with BBCodeView?
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
The RichTextFX has a totalHeightEstimate property. I use this to replace the default TextArea.
 
Upvote 0
Top