Find xy Coordinate of an Element on the Screen

313 views
Skip to first unread message

mafokken

unread,
Oct 21, 2018, 12:28:31 AM10/21/18
to MIT App Inventor Forum
Hey!

Just trying to figure out how to find the x,y coordinates of an element on the screen in App Inventor.
I can find the width and height, but need to know the upper left x,y of the element to do the rest of my logic.

Thanks!

TimAI2

unread,
Oct 21, 2018, 5:17:27 AM10/21/18
to MIT App Inventor Forum
If you mean on a canvas then simply use the X and Y blocks for the ball or sprite from the drawer

If you mean on a normal screen layout, you will need to do some maths, know the dimensions of the screen display, what happens to these dimensions if you use "responsive" sizing (or understand the percentage relationship)
Say you have a component, let us use a label, sized 100x200.
Screen is set to centre / centre
Screen dimensions are 360 wide by 480 high (from screen1.width / screen1.height)

X = 360/2 - 100/2 = 130
Y = 480/2 - 200/2 = 140

From this base you should be able to devise further calculations for more complex layouts

mafokken

unread,
Oct 21, 2018, 5:17:48 PM10/21/18
to mitappinv...@googlegroups.com
I'm not using a Canvas. But that does make sense for the canvas part. 

Is there another example that goes into depth with what you explained in the second example?

Unfortunately, I'm not centering stuff on the screen. I have four square images that I'm going to place on the screen and then detect when people touch them (Not in a canvas). But with what I'm doing I need to be able to figure out one of the x,y coordinates of one of the corners of the image. Is there anything like get X coordinate or get Y coordinate for each type of element?

Because what if there is text above one of the images that is changing and that moves the image up or down on the screen? Then the x,y coordinates will change, right?

Chris Ward

unread,
Oct 21, 2018, 5:38:21 PM10/21/18
to MIT App Inventor Forum
Hi mafokken

All you need to do is place each image on it's own button..........?

You can place the text in a Label Component and that Label in a Vertical Scroll Arrangement such that if the text changes, the screen layout does not.

mafokken

unread,
Oct 21, 2018, 5:58:53 PM10/21/18
to MIT App Inventor Forum
That makes sense. I'm trying to make a bluetooth rc car controller. Using Buttons, I can't do multi-touch. Meaning, I can only press forward or back or left or right. Not left while going forward.


If I can figure out how to determine the x,y coordinates of one of the corners of an image element then I could get multi-touch working for where ever I want to put the buttons. I tried Canvas because then I could find the x,y coordinates of the image, but the multi touch didn't work in the Canvas.

TimAI2

unread,
Oct 21, 2018, 6:30:59 PM10/21/18
to MIT App Inventor Forum
Have you used Juan's multitouch extension?

As Chris says, use buttons for your images (then you get the touch events for the button, which will tell you which button)

You will then have to use the available blocks for screen dimensions, button dimensions etc., to calculate the x,y coordinates on the fly

Chris Ward

unread,
Oct 21, 2018, 8:38:36 PM10/21/18
to MIT App Inventor Forum
Hi mafokken

As Tim says concerning multi-touch, as App Inventor currently does not support multi-touch natively, needs an extension:

Scale Detector. Adds as multitouch scale gesture detector to a Canvas by MIT

Multitouch Extension by Juan Antonio

Multitouch in Canvas. Touch with concurrently fingers and get coordinates and draw points by Juan Antonio

Customize any touchscreen event including any multi touch screen Extension by Zhangzqs

Chris Ward

unread,
Oct 21, 2018, 10:24:32 PM10/21/18
to MIT App Inventor Forum
...or you could use more buttons, thus not needing multi-touch:

9_ButtonGui.png


Reply all
Reply to author
Forward
0 new messages