java - How to get the screen's size of a phone Android -


this question has answer here:

i want size of screen, have resolution

 displaymetrics displaymetrics = new displaymetrics();     windowmanager windowmanager = (windowmanager) getapplicationcontext().getsystemservice(context.window_service);     windowmanager.getdefaultdisplay().getmetrics(displaymetrics);     final int devicewidth = displaymetrics.widthpixels;     final int deviceheight = displaymetrics.heightpixels; 

but want size in inches, samsung galaxy s7 has resolution of 2560x1440 , screen of 5.1" , it's 5.1 want get.

thanks

if want display dimensions in pixels can use code:

display display = getwindowmanager().getdefaultdisplay();  int width = display.getwidth(); int height = display.getheight(); 

then can add condition compares height satisfy needs.

in inches:

displaymetrics dm = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(dm); double x = math.pow(dm.widthpixels/dm.xdpi,2); double y = math.pow(dm.heightpixels/dm.ydpi,2); double screeninches = math.sqrt(x+y); log.d("debug","screen inches : " + screeninches); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -