Alex Rivera | Logout

How to get screen width and height

Asked 2011-06-29T12:36:02.803
55

I tried to use following code to get screen width and height in android app development:

Display display = getWindowManager().getDefaultDisplay(); 
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();

but I got NullPointerException for my display, why? how to get screen width and height then?

Edit
Report

2 Answers

13

Try via context.getResources().getDisplayMetrics() if you have a context available.

answered 2011-06-29T12:37:15.880
0
Display display = getActivity().getWindowManager().getDefaultDisplay(); 
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
Log.d("Tag", "Getting Width >> " + screenWidth);
Log.d("Tag", "Getting Height >> " + screenHeight);

This worked properly in my application

answered 2013-06-19T11:13:05.843

Your Answer