12

I was trying to get the screen resolution of android phones,using this code

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    str_ScreenSize = dm.widthPixels + " x " + dm.heightPixels;
    str_ScreenSize = "dd" + " x " + dm.heightPixels;

When i tried this code in my Galaxy S phone i got the screen resolution as 320x533 px, but in reality the Galaxy S got a screen resolution of 480x800 px. So what's wrong with the code??

How can i get the actual screen resolution of a particular device??

Shijilal
  • 2,175
  • 10
  • 32
  • 55
  • Conclusion about how to get real resolution: http://stackoverflow.com/questions/11824193/htc-sensation-real-resolution – thecr0w Aug 09 '12 at 02:14
  • screen size 600(width) x 1024(height).i get current width 600 but wrong get height 976 (without rotated screen).i get current height 1024 but wrong get width 552 (with rotated screen). please help me. – AnilPatel Apr 22 '13 at 05:55

6 Answers6

23

Finally after 2 days of searching and testing, i finally managed to find out what's the real issue here.. All those above coding will give correct resolution. But the most important this is that we need to mention the target SDK in the manifest file.. Otherwise it will give wrong result if the density is more than 1.

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />

need to set the targetSDK version from anything between 4-9.. Hope this will help some one in future facing the same issue.

Shijilal
  • 2,175
  • 10
  • 32
  • 55
  • I spent all day trying to figure out what was happening; it took a long time to realize that getWindowManager().getDefaultDisplay().getMetrics(dm) was returning the wrong value!! After that, the search was simple. Thanks for saving me some hours! – Thunder Rabbit Jul 13 '11 at 05:32
  • I found that you even need to set this in order for the view size to be setup correctly. Thanks! – Aaron Feb 13 '12 at 01:47
  • 1
    Really bizarre, I was having this problem too but I had targetSdkVersion in the manifest. Realized it should be before the block and sure enough it works now. Thanks – Nick Gotch Feb 24 '12 at 19:36
4

Try to use this:

width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;

Seems like you're getting the pixel after division of the density ( 1.5 ) of Galaxy S.

480 / 1.5 = 320
799.5 / 1.5 = 533

Edit:

density = getResources().getDisplayMetrics().density;
SteD
  • 13,909
  • 12
  • 65
  • 76
  • ohh..So how can i get the density of a device?? – Shijilal Feb 22 '11 at 14:02
  • i am getting the density value for Galaxy S as 1.0 – Shijilal Feb 22 '11 at 14:40
  • 1
    This seems to be working..though i dont know if its a right method.. DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); density=dm.densityDpi; width=(density/160)*dm.widthPixels; height=(density/160)*dm.heightPixels; – Shijilal Feb 22 '11 at 15:44
1

Disable pre-scaling by adding this line to your manifest file:

<supports-screens android:anyDensity="true" />

Position:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="...."
  android:versionCode="1"
  android:versionName="1.0">

<supports-screens android:anyDensity="true" />

<application android:label="@string/app_name" >
...

After that your display will not be scaled anymore, and you also get the correct display resolution.

Codev
  • 1,040
  • 2
  • 14
  • 31
0

Here is how i did it:

Display dis = getWindowManager().getDefaultDisplay();
Point point= new Point();
dis.getSize(point);
int width = point.x;
int height = point.y;

When not in an Activity:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated
Remi
  • 1,289
  • 1
  • 18
  • 52
0

try this..

 Display display = getWindowManager().getDefaultDisplay();
int ScreenHeight = display.getHeight();
int ScreenWidth = display.getWidth();
Tushar Vengurlekar
  • 7,649
  • 8
  • 33
  • 48
0

This seems to be working..though i dont know if its a right method..

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

density=dm.densityDpi; 
width=(density/160)*dm.widthPixels; 
height=(density/160)*dm.heightPixels;
Shijilal
  • 2,175
  • 10
  • 32
  • 55