Search code examples
javaandroidscreen-resolution

Get real screen size in Android


I know the way to get "screen size" using DisplayMetrics, but as note in the SDK Document:

The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display. The returned size may be adjusted to exclude certain system decor elements that are always visible. It may also be scaled to provide compatibility with older applications that were originally designed for smaller displays.

Is there anyway to get the real screen size?


Solution

  • It may not be an answer for your question, but it could be useful to know that if you need a View's dimension as your code is being executed when its layout has not been laid out yet (for example in onCreate() ) you can setup a ViewTreeObserver.OnGlobalLayoutListener with View.getViewTreeObserver().addOnGlobalLayoutListener() and put the relevant code that needs the view's dimension there. The listener's callback will be called when the layout will have been laid out.

    For more details visit : ViewTreeObserver

    But for general screen dimension we use DisplayMetrics itself though ignoring the SDK document note.