4

On Android 2.1 emulator.
In an ActivityInstrumentationtestCase2 test class,
I am asserting 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 was expecting different values from these methods.

Why are getLocationOnScreen() and getLocationInWindow() returning same values ?

user77115
  • 5,517
  • 6
  • 37
  • 40
  • Did you take a look at [this question](http://stackoverflow.com/questions/2638342/incorrect-coordinates-from-getlocationonscreen-getlocationinwindow)? – Knickedi Oct 09 '11 at 20:48
  • Re-phrase: Is it OK/normal that these methods return the same value ? Am I misinterpreting the meaning of Window and Screen ? As I understand it they should always return different values. – user77115 Oct 09 '11 at 21:30

1 Answers1

4

I was confused by this as well, so I did a bit of investigation that I summarize here.

Basically, the window lays out beneath the status bar (beneath in regards to z-order not y-coordinates), and fills the entire screen, in most cases. So, in a normal activity you should expect these methods to return the same values. Only in unique cases, such as dialogs where the Window is actually offset, will you see these methods returning different values.

Community
  • 1
  • 1
groucho
  • 1,835
  • 1
  • 13
  • 15