Why do getLocationOnScreen () and getLocationInWindow () return the same value?

On the emulator Android 2.1.
In the test class ActivityInstrumentationtestCase2,
I claim that phototButton is above sendButton.

@UiThreadTest public void testViewLocationOnScreen() { // Trying to trigger layout activity.findViewById(R.id.rootSnap).forceLayout(); activity.findViewById(R.id.rootSnap).requestLayout(); activity.photoButton.getRootView().requestLayout(); activity.photoButton.requestLayout(); activity.photoButton.invalidate(); activity.onWindowFocusChanged(true); // Successfull asserts assertTrue(activity.hasWindowFocus()); ViewAsserts.assertOnScreen(activity.photoButton.getRootView(), activity.photoButton); ViewAsserts.assertOnScreen(activity.sendButton.getRootView(), activity.sendButton); activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Assert.assertTrue(activity.photoButton.isShown()); Assert.assertTrue(activity.sendButton.isShown()); // Unexpected screen coordinates returned from // getLocationOnScreen() and getLocationInWindow() int[] above = new int[2]; activity.photoButton.getLocationOnScreen(above); int[] below = new int[2]; activity.sendButton.getLocationOnScreen(below); log("getLocationOnScreen-above", above); log("getLocationOnScreen-below", below); // Logs screen coodinates [0, 76] and [0, 178] above = new int[2]; activity.photoButton.getLocationInWindow(above); below = new int[2]; activity.sendButton.getLocationInWindow(below); log("getLocationInWindow-above", above); log("getLocationInWindow-below", below); // Logs window coordinates [0, 76] and [0, 178] } 

I expected different values ​​from these methods.

Why do getLocationOnScreen () and getLocationInWindow () return the same value?

+4
source share
1 answer

I was also confused by this, so I did a little investigation, which I summarized here .

In principle, the window is displayed under the status bar (below with respect to the z-order of non-y coordinates) and in most cases fills the entire screen. Thus, with normal activity, you should expect these methods to return the same values. Only in unique cases, such as dialogs, where the Window is actually shifted, you will see that these methods return different values.

+4
source

All Articles