ab11
ab11

Reputation: 20100

Android: how to find size of viewable screen?

I was going to use Display.getWidth() x Display.getHeight(), but the documentation for getHeight() notes:

Returns the raw height of the display, in pixels. Note that this should not generally be used for computing layouts, since a device will typically have screen decoration (such as a status bar) along the edges of the display that reduce the amount of application space available from the raw size returned here. This value is adjusted for you based on the current rotation of the display.

So, what is it reliable to do Display.getWidth() x Display.getHeight() - statusHeight? Or is there a more formal way to find the viewable rectangle?

Upvotes: 0

Views: 1108

Answers (2)

adamp
adamp

Reputation: 28932

Use View#getWidth() and View#getHeight() on the relevant View instance after layout is complete. If you are using a SurfaceView you can also use the values supplied to you by the surfaceChanged callback.

Upvotes: 4

Malcolm
Malcolm

Reputation: 41498

It all depends on what you're trying to do. If you are creating a SurfaceView which fills the screen, you should implement SurfaceHolder.Callback and find out the size when surfaceChanged() is called. If you're creating your own view, then you determine your size in the onMeasure() method.

Upvotes: 1

Related Questions