Android's FULL screen size
Asked Answered
O

2

6

I want to get Android device's screen size. I'm using these 3 codes to do this but these are giving me the activity's size; which is without the size of the status bars height. On tablet with 800px height, this code gives 764px. 36px goes to status bar on the bottom.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    final int width = size.x;
    final int height = size.y;



    Rect rectgle= new Rect();   
    Window window= getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int height2= rectgle.bottom;



    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int actualHeight = metrics.heightPixels;

But I need a code which gives me the actual size of the screen. Not the activity's. can anyone help me about this? Thank you.

Obryan answered 25/8, 2013 at 14:6 Comment(0)
S
5

For API Level 17 and above you can use getRealSize()

https://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)

Seamus answered 25/8, 2013 at 14:58 Comment(3)
This is a bit noobish but, actually I couldn't understand how can I create a Display object in my class? There is no constructor.Crystallo
you can gather display object from window manager via this Display display = getWindowManager().getDefaultDisplay();Seamus
Yeah. I found 10 sec before your answer =) Yeah. It's done now. Thank you.Crystallo
F
3

This snippet will give you height of your status bar.

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;

So the total height of screen is :-

height = height+TitleBarHeight;

original answer is here. Also note this will not work in onCreate put that code in runnable.

Firenew answered 25/8, 2013 at 14:9 Comment(1)
I saw this answer. The problem is this: device is a TABLET, not a phone. So status bar is on bottom, not top. This code gives me 0px for StatusBarHeight. When I changed the int StatusBarHeight= rectgle.top; code to int StatusBarHeight= rectgle.bottom; now I'm getting activityHeight. Which is 764px. So to get StatusBarHeight; I still need the screen's actual size.Crystallo

© 2022 - 2024 — McMap. All rights reserved.