How can I get the size of the android application window and not the physical screen size?
Asked Answered
F

2

11

In Android, how can I get the size of the app window? I'm NOT looking for the physical screen size, but the actual size of the applications window on the screen. E.g. generally what's left after you take away the notification bar, soft touch buttons, etc.

Funerary answered 30/11, 2012 at 23:53 Comment(0)
V
13

If you want the size of your app's window you can just call yourview.getHeight() and yourview.getWidth(); But the view must be drawn for that to work.

If you want to get the height before it's drawn you can do this:

https://mcmap.net/q/82161/-android-get-height-of-a-view-before-it-180-s-drawn

There are a lot of different cases of screen/view measurement though, so if you could be more specific about "actual size of applications window", is this your app? Or the home screen app?, some other app?

If you want usable space minus decorations (status bar/soft button), you can attack it in reverse as well, for example measuring the actual screen dimensions (the methods vary by API), and then subtract the height of the status bar.

Example display height:

// Deprecated
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();

status bar height:

Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;

Or

int statusBarHeight = Math.ceil(25 * context.getResources().getDisplayMetrics().density);

Many different ways to do this.

Certainly need more info from you though to give a more accurate answer.

Verduzco answered 1/12, 2012 at 0:4 Comment(5)
Thanks, its my own activity dimensions I'm after. Parent view width/height would do it for me but as you say needs the view drawn first.Funerary
Cool, well the link I provided should do it for you, but you can ignore the part about hiding after measurement. Alternatively just call .getHeight() and .getWidth() after the view is drawn (example in an activity you've already called setContentView(R.layout.yourview);Verduzco
"If you want to get the height before it's drawn you can do this:" - Then you provide a link to some code that can only determine the dimension AFTER the view has been inflated.Guimpe
this is madness on Google's end -- the UI should generally be an afterthought for everyone except graphic designers; I applaud Google for granting fairly low level capabilities to applications, but they forgot that it is almost as important to make common/necessary things like the UI 'just work' if devs don't feel like diving into a random window system. Also notable, the docs at developer.android.com/reference/android/view/Display.html are just plain wrong for some devices. I'm running 4.2.2 on a Huawei Y320 and getRealMetrics() and getMetrics() return exactly the same resultsMagically
My article might help you it show how to work with Window-size-classes mubaraknative.medium.com/…Valency
D
10

This is also pretty easy:

Rect windowRect = new Rect();

yourView.getWindowVisibleDisplayFrame(windowRect);

int hWindow = windowRect.height();
int wWindow = windowRect.width();
Dannica answered 19/12, 2014 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.