Android Question Get .Top and .Left positions for Buttons created with relative positions

Batman_

Member
Licensed User
Hi.

I have a Designer script that positions some Buttons with "Relative" positioning and I want to animate them using the Animation library. For this I require the .Top and .Left positions as I want to animate them horizontally and not from the top left of the screen. Note: .Left position isn't really required as I will use 0 but the principle holds. The Designer script code for the positioning of the Imageviews:
B4X:
IconButton_A.VerticalCenter=40%y
IconButton_B.Top=IconButton_A.Bottom + 25dip
IconButton_C.Top=IconButton_B.Bottom + 25dip
IconButton_D.Top=IconButton_C.Bottom + 25dip
However when I run this code to get the co-ordinates I only get the .Top from the first Button and not the other Buttons.
B4X:
'Refresh UI so can calculate location of Buttons
    IconButton_A.Invalidate
    IconButton_B.Invalidate
    IconButton_C.Invalidate
    IconButton_D.Invalidate
 
    I=0
    For I= 0 To 3
        LogColor("Sub: SecondScreen.Activity_Create(). IconButtonCoordinates Array - Button:= " & Starter.IconButtonCoordinates(I,0),Colors.Blue)
        LogColor("Sub: SecondScreen.Activity_Create(). IconButtonCoordinates Array - Top:= " & Starter.IconButtonCoordinates(I,1),Colors.DarkGray)
        LogColor("Sub: SecondScreen.Activity_Create(). IconButtonCoordinates Array - Left:= " & Starter.IconButtonCoordinates(I,2),Colors.Magenta)
    Next
In the log I get returned (see attached image).

I believe it is some kind of timing issue as the Buttons draw correctly (see attachment) on screen in their relative positions (as per Designer script). I just can't get access to the positions myself.

I believe a Callback is required (read on a java forum)? But I don't know what to do and I have no java knowledge. See attachment for Java answer to my problem.
 

Attachments

Last edited:

Erel

Administrator
Staff member
Licensed User
No callback is required. The positions should be correct immediately after the call to LoadLayout.
 

Batman_

Member
Licensed User
Hi.

I have no issue with the icon buttons positioning correctly.

I wanted to get the .Top position of each Icon Button so I can use the Animation library to animate them horizontally and not from the top left of screen. I want the icon buttons to appear to move sideways from the left of screen not from the top left of screen. For this I needed the .Top position for each Icon Button to set the start position of each Icon Button.

Anyhow, it does seem to be an issue with timing and not being able to get correct co-ordinates to store in an array (or single variables).

I have now put a time delay using this function (see code):
B4X:
Sub Delay(nMilliSecond As Long)
    Dim nBeginTime As Long
    Dim nEndTime As Long
    nEndTime = DateTime.Now + nMilliSecond
    nBeginTime = DateTime.Now
    Do While nBeginTime < nEndTime
        nBeginTime = DateTime.Now
        'Log(nBeginTime)
        If nEndTime < nBeginTime Then
            Return
        End If
        DoEvents
    Loop
End Sub
and set it to 5 nMilliSecond.

By using this time 5 nMilliSecond delay I can get the array (and/or single variables) to get the .Top correctly with the .Top value of each Icon Button rather than populate with 0 that happens without the delay.

I don't know if this is a hack or what I should be using but it works.
 

Erel

Administrator
Staff member
Licensed User
I don't know if this is a hack or what I should be using but it works.
You should never use DoEvents.

You also don't need to use the animation library. Just call view.SetLayoutAnimated.

The simplest way for us to help you is if you upload a small project with the layout.
 
Top