Like Azlam , you can use View.getLocationInWindow () to get the x, y coordinates .
Here is an example:
Button button = (Button) findViewById(R.id.yourButtonId); Point point = getPointOfView(button); Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")"); private Point getPointOfView(View view) { int[] location = new int[2]; view.getLocationInWindow(location); return new Point(location[0], location[1]); }
Bonus To get the center point of a given view:
Point centerPoint = getCenterPointOfView(button); Log.d(TAG, "view center point x,y (" + centerPoint.x + ", " + centerPoint.y + ")"); private Point getCenterPointOfView(View view) { int[] location = new int[2]; view.getLocationInWindow(location); int x = location[0] + view.getWidth() / 2; int y = location[1] + view.getHeight() / 2; return new Point(x, y); }
I hope this example can be useful to someone.
source share