tags:

views:

67

answers:

2

I need to obtain the screen resolution, width & height, how do I do that?

Solution:

    Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();


    width = d.getWidth();
    height = d.getHeight();
+1  A: 
Display d=Display.getInstance();
int width = d.getDisplayWidth();
int height = d.getDisplayHeight();

According to http://forums.java.net/jive/message.jspa?messageID=479230

or possibly

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth(); 

http://www.coderanch.com/t/435390/Android/Mobile/screen-size-at-run-time

I82Much
I am getting an error: The method getInstance() is undefined for the type Display. Could you also show what to import?
tylercomp
works now, with this:Display d= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); width = d.getWidth();height = d.getHeight();Thanks!
tylercomp
+2  A: 

PLEASE first ask yourself why you need to do this. You very probably don't. If it is to do anything based on the screen size (layout etc) this will return you the wrong information, since it is telling you about the physical resolution of the screen, not taking into account anything like the status bar, window decorations, etc. Even if you are running your app as full-screen, in some cases there may be parts of the screen that are used for system UI that you can't get rid of but will be included in the numbers returned by Display.

The correct thing to do is adjust for the size your view gets during layout, such as when its onSizeChanged() is called.

hackbod