Can you get the true height of a view set to wrap contents?

kalisco

Banned
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim Button1 As Button
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Button1.Initialize("Button1")   
   Activity.AddView(Button1,0,0,0,0)
   Button1.Text="20"
   Button1.TextSize=20
   'Fill Parent Width
   Button1.Width=-1
   'Wrap Contents
   Button1.Height=-2
   'I want Button1 at the bottom of the screen, but to do this I would need the 
   '   real height of Button1
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   If Button1.TextSize=20 Then
      Button1.TextSize=40
   Else
      Button1.TextSize=20
   End If
   Button1.Text=Button1.TextSize
   'The Button will change height because the height is wrapped to contents
   'Because Button1.Height is set to -2, I can't get the height of Button1 in order
   '   to keep it at the bottom of the screen 
End Sub

I am using -2 as the height of a button to make it wrap its contents.
My problem then is that I can not find a way of obtaining the real height of the button, in order to adjust its layout.
 

NJDude

Expert
Licensed User
Longtime User
I haven't tested this code, doing it from the top of my head but it might work:
B4X:
Sub Button1_Click
    If Button1.TextSize=20 Then
        Button1.TextSize=40
    Else
        Button1.TextSize=20
    End If
    Button1.Text=Button1.TextSize
    'The Button will change height because the height is wrapped to contents
    'Because Button1.Height is set to -2, I can't get the height of Button1 in order
    '    to keep it at the bottom of the screen End Sub

    Button1.Height = Button1.TextSize * 5

    Button1.Top = Activity.Height - Button1.Height

End Sub
 
Last edited:
Upvote 0

kalisco

Banned
Afraid not NJDude. That just gives me a much bigger button.
What I need to know is how the height of the button is related to the text size.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
What you need is: GetMeasuredHeight()

I have NOT tested this.
You can use Reflection to get this:
B4X:
Sub GetRealHeight(b as Button) As Int
Dim r as Reflector
r.Target = b
Dim h as Int
h = r.RunMethod("getMeasuredHeight")
Return h
End Sub

EDIT: Klaus' solution is probably the correct one. getMeasuredHeight will not always work.
 
Last edited:
Upvote 0
Top