1

i am trying to ge the status bar height and it successfully works in 2.3. But when i run the same code on 4.0+, it will return as 0.

here is the code i am using...

Rect rectgle= new Rect();
                Window window= getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
                int StatusBarHeight= rectgle.top;
                int contentViewTop= 
                    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                int TitleBarHeight= contentViewTop - StatusBarHeight;


                Context context = getApplicationContext();
                CharSequence text = "DETERMINED STATUSBAR HEIGHT - "+TitleBarHeight;
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

Any input would be awesome!

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
user1190019
  • 565
  • 1
  • 5
  • 17
  • 1
    This doesn't work on any API version. `StatusBarHeight` always equals 0 since `getWindowVisibleDisplayFrame(rect)` gives the entire screen, so `top` always equals 0. I suggest you step through you code in the debugger. – techi.services Jun 10 '12 at 21:21
  • I return me 0 when i tried to obtain in onCreate,because the layout doesn't know its size yet in onCreate because it hasn't finished being drawn. What I usually do is post a Runnable on the UI thread from onCreate which gives the UI time to draw itself. Like this i was able to get the status bar height in all versions. – Dory Sep 21 '13 at 07:07

0 Answers0