Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android .getTop() returns 0.0

Tags:

java

android

xml

I am developing an android lock screen app. I got the idea from another thread to check the coordinates of where the finger has pressed down against to the coordinates of the imagebutton that I have set up.

I am trying to get the coordinates of the imagebutton using the .getTop() and .getLeft() but both are returning 0.0 (as they are floats, not int).

why is this? I am new to android development, so sorry if I am missing something obvious.

Also, I read somewhere that the .getTop() won't work after the setContentView(R.layout.layoutname) but if I put it above setContentView(...) then the app crashes.

Is there another way to get the coordinates of the imagebutton, or would it be better/easier to get the coordinates and bounds of the table cell ? How would I do that?

Thanks in advance :)

like image 249
VaMoose Avatar asked Jan 08 '14 09:01

VaMoose


2 Answers

You need your view added to a layout and displayed (At least one layout pass and measure pass must have run.). Also, the co-ordinates so obtained are relative to view's immediate parent.

It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view. These methods both return the location of the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.

like image 70
S.D. Avatar answered Oct 14 '22 23:10

S.D.


imagebutton.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

try this before imagebutton.getTop()?

like image 38
venciallee Avatar answered Oct 15 '22 00:10

venciallee