5

I've got an Activity which uses

getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);

to determine the useable screen space and decide where to place images.

Returning to the Activity after I click the hardware "back" button to leave the Activity, the rectangle values are

(0,0,800,480)

However, returning to the Activity after I click the hardware "home" button to leave the Activity, the rectangle values are

(0,38,800,480)

which throws off the display and the image placement.

How can I ensure I get a consistent values when calling

getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);

no matter how I left the app?

UPDATE: Thanks to @Reno for helping test; it seems to be dependent on Android version than the device.

Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82

4 Answers4

4

Welp, if you read the comments in the source, it admits that this method is kind of broken

    public void getWindowVisibleDisplayFrame(Rect outRect) {
    if (mAttachInfo != null) {
        try {
            mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
        } catch (RemoteException e) {
            return;
        }
        // XXX This is really broken, and probably all needs to be done
        // in the window manager, and we need to know more about whether
        // we want the area behind or in front of the IME.
        final Rect insets = mAttachInfo.mVisibleInsets;
        outRect.left += insets.left;
        outRect.top += insets.top;
        outRect.right -= insets.right;
        outRect.bottom -= insets.bottom;
        return;
    }

You will have to ignore the outRect.top value for versions < 2.3.3

Reno
  • 33,594
  • 11
  • 89
  • 102
  • Why ignore outRect.top for versions < 2.3.3? I'm seeing non-zero values when running on versions > 2.3.3 (xoom, nexus 7...), which takes the top status bar into account. I don't have a device that's running < 2.3.3 but when I run the emulator I see the same thing. I'm just looking to get the current height and width of the usable area and getWindowVisibleDisplayFrame() seems consistent, but it sounds like you've seen something different. Thanks. – SkolVikingsGuy Mar 04 '13 at 13:48
3

here is a link to get status bar directly instead of calculate it. It will avoid any issue of getWindowVisibleDisplayFrame which is really inconsistent through platforms and devices.

superuser
  • 768
  • 7
  • 9
1

There are times when you need to know the precise dimensions of the available space for a layout when in an activity's onCreate. After some thought I worked out this way of doing it: https://stackoverflow.com/a/16691531/2202013 It does not use getWindowVisibleDisplayFrame and is hopefully future proof.

Community
  • 1
  • 1
Steve Waring
  • 2,882
  • 2
  • 32
  • 37
0

So, I did this:

    Rect rectangle = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
    if(android.os.Build.VERSION.SDK_INT < 9  /* android.os.Build.VERSION_CODES.GINGERBREAD */ ) {
        // http://stackoverflow.com/questions/7659652/getwindowvisibledisplayframe-gives-different-values-in-android-2-2-2-3-but-no/7660204#7660204
        rectangle.top = 0;
    }
Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82
  • I'm not exactly following the issue, but I'd like to use getWindowVisibleDisplayFrame() to get the area available for my application minus all status bars. Right now I'm calling getWindow()getDecorView().getWindowVisibleDisplayFrame() for all versions of android and it works fine, with a non-zero 'top' value when there's a status bar at the top. Will this code be reliable on all versions of android? Note: I'm calling that when the user hits a button. – SkolVikingsGuy Mar 03 '13 at 17:20
  • You mentioned "UPDATE: Thanks to @Reno for helping test; it seems to be dependent on Android version than the device.", could you help me out and let me know what the differences were in versions? I'm just nervous that one version will return the screen size without status bars and one will return the rect with status bars. Thanks. – SkolVikingsGuy Mar 04 '13 at 13:36
  • I'm sorry; I cannot. I haven't worked on Android in over a year; this post was from 18 months ago. I simply don't remember. :-( You're probably better off asking a new question. :-) – Thunder Rabbit Mar 05 '13 at 08:07