Reputation: 332
I trying to get x,y for any object inside the layout but I got this values when object at end corner of the screen :
Object X : 266
Object Y : 361
Screen Width : 320
Screen Height: 480
how can I know where is the object exactly for the screen? (end,top,left,center).
Upvotes: 2
Views: 9311
Reputation: 598
Button b;
int x1 = b.getX();
int x2 = x1 + b.getWidth();
int y1 = b.getY();
int y2 = y1 + b.getHeight();
Upvotes: 1
Reputation: 773
Hope this helps..
public Rect locateView(View view) {
Rect loc = new Rect();
int[] location = new int[2];
if (view == null) {
return loc;
}
view.getLocationOnScreen(location);
loc.left = location[0];
loc.top = location[1];
loc.right = loc.left + view.getWidth();
loc.bottom = loc.top + view.getHeight();
return loc;
}
And I used the method this way:
Rect r = TVGridUtils.locateView(activity.findViewById(R.id.imageview));
float touchX=r.left+((r.right-r.left)/2);
float touchY=(r.bottom);
Note: I had to get the middle point of the imageview which i touched
Upvotes: 18
Reputation: 9035
I think you need to add the width
and height
of the objects to x
and y
values
To know where is the object exactly for the screen
X= 0,Y= 0 TOP LEFT
X= screenWidth-objectWidth,Y= 0 TOP RIGHT
X= 0,Y= screenHeight-objectHeight BOTTOM LEFT
X= screenWidth-objectWidth,Y= screenHeight-objectHeight BOTTOM RIGHT
Upvotes: 1