View view = findViewById(R.id.myView); int[] location = new int[2]; view.getLocationInWindow(location); Log.d("TAG", "View location in window: " + location[0] + ", " + location[1]);
View view1 = findViewById(R.id.myView1); View view2 = findViewById(R.id.myView2); int[] location1 = new int[2]; int[] location2 = new int[2]; view1.getLocationInWindow(location1); view2.getLocationInWindow(location2); int distance = Math.abs(location1[0] - location2[0]); Log.d("TAG", "Distance between views: " + distance);This example demonstrates how to calculate the distance between two views in their window by getting the location of each view using the getLocationInWindow() method. The Math.abs() method is used to calculate the absolute distance between the two x-coordinates. The distance is then logged to the console using the Log.d() method. Package Library: android.view