Jyosna
Jyosna

Reputation: 4446

issue with screen resolution

Through programming how can i know whats the screen resolution of my screen. As soon as run the app it should find out the screen resolution in dp and in pixel.

How can do that?

Thank you

Upvotes: 0

Views: 185

Answers (2)

anujprashar
anujprashar

Reputation: 6335

Use below code to get screen width & height

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;

Also dont forget to add

<uses-sdk android:minSdkVersion="4"/>

in your manifest. The value of minSdkVersion be equal or above 4. But if your minSdkVersion is 3 then your targetSdkVersion should be equal or above 4

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

to have correct value of width & height.

Upvotes: 1

user370305
user370305

Reputation: 109237

Use, DisplayMetrics to get screen info from your device.

Some sample code,

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

final int height = dm.heightPixels;
final int width = dm.widthPixels;

EDIT:

float xDpi =   dm.xdpi;
float yDpi =   dm.ydpi;

float density = dm.density;

Upvotes: 1

Related Questions