Android Question Views Position (without Designer)

zakker

Member
Licensed User
Hi to you all, I've got a problem positioning views runtime.

Activity.AddView(Panel1,0,0,100%x,100%y)
Panel1.AddView(Label1,0,2dip,100%x,-2)
Panel1.AddView(Spinner1,0,Label1.Top+Label1.Height+1dip,100%x-2dip,40dip)

I use -2 in label to auto-height the label. I'd like positioning Spinner1 below the Label1 but with this code both Spinner and Label have the same TOP.

If I use this code, instead, it works as I want.

Activity.AddView(Panel1,0,0,100%x,100%y)
Panel1.AddView(Label1,0,2dip,100%x,35dip)
Panel1.AddView(Spinner1,0,Label1.Top+Label1.Height+1dip,100%x-2dip,40dip)

Some solution?

I don't want fixed height for label !!!!!!
 

Cableguy

Expert
Licensed User
Longtime User
You should reposition the spinner in the label's size changed event
(Not sure of the event name, it could be resized)

A separate sub and call it with callsubdelayed
 
Upvote 0

zakker

Member
Licensed User
You should reposition the spinner in the label's size changed event
(Not sure of the event name, it could be resized)

A separate sub and call it with callsubdelayed

Found a solution using Reflector as indicated by @LucaMs user

B4X:
Sub CalcolaDimensione(tipo As String, oggetto As Object) As Int
   Dim Reflect As Reflector
   Dim Height, Width As Int
  
   Reflect.Target=oggetto
   If tipo="H" Then
     Height = Reflect.RunMethod("getHeight")
     Log(Height)
     Return Height
   Else
     If tipo ="W" Then
       Width = Reflect.RunMethod("getWidth")
       Log(Width)
       Return Width      
     End If
   End If
End Sub
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User

You could do it a little better.

1) in English
2) as you can see, you get a warning ("Not all code paths return a Value"). It is better to use a single "Return xxx" statement, before the End Sub.
3) Comments you write before the name of the Sub will be shown as documentation.

B4X:
' Returns the Height or Width of an auto sized view.
' USES: Reflection library.
'    pSide: pass h or w (for height / width)
'    pView: the auto-sized view.
Sub GetViewSize(pSide As String, pView As Object) As Int
    Dim Result As Int
    Dim Reflect As Reflector

    pSide = pSide.ToLowerCase

    Reflect.Target = pView

    If pSide = "h" Then
        Result = Reflect.RunMethod("getHeight")
    Else
        If pSide = "w" Then
            Result = Reflect.RunMethod("getWidth")
        End If
    End If

    Return Result
End Sub

[My bad english prevents me to choose the "right" name for the parameter pSide, I think ]
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…