27

Is there a way to obtain the size of the notification bar and title bar in android? At the moment I obtain the display width and height with:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

After that I want to subtract the sizes of the bars so that I can stretch a video without losing aspect ratio. Currently I hide the bars because I can't see a better way.

Community
  • 1
  • 1
lalalei
  • 323
  • 1
  • 3
  • 6

8 Answers8

20

Maybe this is a helpful approach: Referring to the Icon Design Guidelines there are only three different heights for the status (notification) bar depending on the screen density:

  • 24px for LDPI
  • 32px for MDPI
  • 48px for HDPI

So if you retrieve the screen density of the device using densityDpi of DisplayMetrics you know which value to subtract

so it could look something like that:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int myHeight = 0;

    switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_HIGH:
            Log.i("display", "high");
            myHeight = display.getHeight() - 48;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            Log.i("display", "medium/default");
            myHeight = display.getHeight() - 32;
            break;
        case DisplayMetrics.DENSITY_LOW:
            Log.i("display", "low");
            myHeight = display.getHeight() - 24;
            break;
        default:
            Log.i("display", "Unknown density");
tokentoken
  • 682
  • 7
  • 15
DonGru
  • 13,532
  • 8
  • 45
  • 55
  • This looks promising. I tried to implement it, but even though densityDpi is declared public in the javadoc, I can't access it in Eclipse... Using density instead does not seem to be an alternative, as the javadoc says: "This value does not exactly follow the real screen size " – lalalei Sep 01 '10 at 09:15
  • Thanks! I didn't realize that metrics.densityDpi is not available in Android 1.5. Now I change my project to use 1.6 and it works! I just changed one more thing: I multiplied the values by 2, e.g. display.getHeight() - 48 * 2. By that I substract the height of the titlebar AND the notification bar. Hope this holds for other devices too. Just tested it on the Nexus. – lalalei Sep 01 '10 at 10:56
  • I think it should, because the specification says so - would be great if you could accept the answer by clicking the tick icon on the left :) – DonGru Sep 01 '10 at 11:47
  • Notification bar height in dp 24dp – sergey.n Sep 16 '14 at 09:22
19

*As for as I know, this works perfectly.

public int getStatusBarHeight() {
            int result = 0;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
            return result;
        }*
sh0m1
  • 536
  • 4
  • 7
18

Martin's answer specified the wrong height (at least as of SDKv11). As per Icon Design Guidelines, the status bar icons have a height of 25dp, not 32dp. 25dp translates into these density-specific heights:

  • 19px for LDPI
  • 25px for MDPI
  • 38px for HDPI
  • 50px for XHDPI

An easy way to observe these sizes is to use the hierarchyviewer against an emulator or device with a normal density. Simply look at the value of getHeight() for the title bar's FrameLayout. The status bar is a bit trickier because it's part of the DecorView and not a view in itself, so get its height indirectly by looking at mTop for the title bar's FrameLayout, which is positioned immediately below the status bar. Per my observations the status bar and title bar each match the 25dp height of the status bar icons (I have not seen a declaration of this fact in the android ref as of SDKv11).

cdhabecker
  • 1,693
  • 1
  • 14
  • 23
3

I use the following code for getting heights:

For Status (Notification) bar:

View decorView = getWindow().getDecorView();
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;

For Title bar:

View contentView = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int[] location = new int[2];
contentView.getLocationInWindow(location);
int titleBarHeight = location[1] - statusBarHeight;
Ashwin
  • 7,277
  • 1
  • 48
  • 70
1

hi i think that isn´t necessary and that will be automatically if you use a VideoView

VideoView vv = (VideoView) findViewById(R.id.MainVideo);                        
    MediaController mc=new MediaController(this);
    mc.setEnabled(true);
    mc.show(0);
    vv.setMediaController(mc); 
    vv.setVideoURI(Uri.parse(URLMedia));
    vv.requestFocus();
    vv.showContextMenu();
    vv.start();         
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • I use SurfaceView instead of VideoView because I implemented different UI for the control elements (I needed a volume control and a switch-fullscreen-button). I dont use MediaController. – lalalei Sep 01 '10 at 09:16
0

sorry for dummy comment, but that's no correct to use constants as height of notification bar. just get the global view (PhoneWindow$DecorView) then get it's child (it'll be only one view (FrameLayout)). this layout contain You'r app views and measure whole screen (@see FrameLayout.mHeight). the first layout of it will be You'r first view, so getting the top of it will give You the right notification bar's height. Using FrameLayout without it's context will bring you nothing, cuz the top of you'r child will be equal to 0 in that case.

for lazy one's :

ViewGroup decoreView = (ViewGroup)*YourActivityViewHere*.getRootView();
int barSize = ((ViewGroup)decoreView.getChildAt(0)).getChildAt(0).getTop();

you can also use

getWindow().getDecorView().getRootView()

instead of YourActivityViewHere

Dvide Leprechaun
  • 346
  • 4
  • 11
0

This works waaaay better than hardcoding values because android will still return the value that would be the height of the status bar or action bar on that particular device even though they may not be visible.

So, the idea is to get the content view onto which all of your views are added.

public View getContentView(Activity a) {
        int id = a.getResources().getIdentifier("content", "id", "android");
        return a.findViewById(id);
    }

Then, in your activity

View cView = getContentView(this);
cView.post(()->{ 
int offsetY = cView.getTop(); 
// do whatever here.
});

The good thing with the above code is that it'll also account for the action bar.

TheRealChx101
  • 1,468
  • 21
  • 38
0

i have found a tutorial may be it would be helpful for u helpful for me thou! How to increase the size of the title bar in an Android application? you can change your title bar size and the way you want it you can get it lucky chap ! have Fun

Community
  • 1
  • 1
Raghav Chopra
  • 527
  • 1
  • 10
  • 26