6

I used following method to get the screen size:

public static Point getScreenSize(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int w = wm.getDefaultDisplay().getWidth();
    int h = wm.getDefaultDisplay().getHeight();
    return new Point(w, h);
}

On my samsung galaxy s6, this method returns 1080x1920... Although my device has a resolution of 1440x2560.

Why? Is there a better method to get the screen resolution that works on newer phones reliable as well?

EDIT

I need this method in a service! I don't have a view/activity for help, only the application context! And I need the REAL pixels

prom85
  • 16,896
  • 17
  • 122
  • 242
  • I had a scrollview as my root for one of the activities in which I needed this. I simply used to get `scroll.getHeight()` and `scroll.getWidth()` on it. No hassles at all and was perfect. – Shaishav Jul 27 '16 at 14:01
  • Check one of the answers at this post: http://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android. There should be one valid for your case. – Bram Jul 27 '16 at 14:02
  • I believe **getWidth()** and **getHeight()** are deprecated in favor of **Point**. – IgorGanapolsky Feb 13 '17 at 16:25

6 Answers6

8

The best way to get your screen resolution is from DisplayMetrics :

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
Lubomir Babev
  • 1,892
  • 1
  • 12
  • 14
7

If you are targeting for API >= 17, try with the getRealSize method of the Display class:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getRealSize(size);
String resolution = size.x + "x" + size.y;

for me, it worked.

Domenico
  • 1,331
  • 18
  • 22
2

Use this one:

private static String getScreenResolution(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    return "{" + width + "," + height + "}";
}
ViramP
  • 1,659
  • 11
  • 11
  • returns the same values... I really need to know the real physical size of the device – prom85 Jul 27 '16 at 14:27
  • You can't get device physical size in all device because that depends on device as well. ie. Check two devices Nexus 5 Lenovo K3 both size is 1080X1920 but when you call above function then get different values. so you check hardware button with device and marge those height with device height so at end you can get actual physical size which manufacture displaying at product. – ViramP Jul 28 '16 at 13:36
  • How can i call this method from my class – A P Jan 09 '17 at 22:02
  • WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); int width = metrics.widthPixels; int height = metrics.heightPixels; – ViramP Jan 10 '17 at 06:08
1

Use this.

DisplayMetrics metrics; 
int width = 0, height = 0;

In your onCreate method.

 metrics = new DisplayMetrics();        
 getWindowManager().getDefaultDisplay().getMetrics(metrics);


 height = metrics.heightPixels;     
 width = metrics.widthPixels;
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
1

The API returns the number of pixels an app can use, so this is the correct result.

Jenis Kasundra
  • 583
  • 9
  • 19
0

Try this:

 Display mDisplay = context.getWindowManager().getDefaultDisplay();

 mDisplay.getWidth();
 mDisplay.getHeight();

Note that this code uses deprecated APIs.

Domenico
  • 1,331
  • 18
  • 22
Quantum4U
  • 35
  • 4