android - why it gives screen inch size wrong all the time? -


i want calculate device size inch. using code every search appears. problem when put device 4.5 inch answer in android studio 4. tried 5.2 inch device , got 4.3 inch 10.1 inch -> answer 9.0. so how can accurate answer?

 displaymetrics dm = new displaymetrics();     getwindowmanager().getdefaultdisplay().getmetrics(dm);      double wi=(double)dm.widthpixels/dm.xdpi;     double hi=(double)dm.heightpixels/dm.ydpi;     double x = math.pow(wi,2);     double y = math.pow(hi,2);     double screeninches = math.sqrt(x+y);     log.e("hello"," "+screeninches); 

the problem code not take account

actionbar

softkeys

orientation of screen

use following answer

 windowmanager windowmanager = getwindowmanager(); display display = windowmanager.getdefaultdisplay(); displaymetrics displaymetrics = new displaymetrics(); display.getmetrics(displaymetrics);  // since sdk_int = 1; mwidthpixels = displaymetrics.widthpixels; mheightpixels = displaymetrics.heightpixels;  // includes window decorations (statusbar bar/menu bar) if (build.version.sdk_int >= 14 && build.version.sdk_int < 17) {     try     {         mwidthpixels = (integer) display.class.getmethod("getrawwidth").invoke(display);         mheightpixels = (integer) display.class.getmethod("getrawheight").invoke(display);     }     catch (exception ignored)     {     } }  // includes window decorations (statusbar bar/menu bar) if (build.version.sdk_int >= 17) {     try     {         point realsize = new point();         display.class.getmethod("getrealsize", point.class).invoke(display, realsize);         mwidthpixels = realsize.x;         mheightpixels = realsize.y;     }     catch (exception ignored)     {     }    displaymetrics dm = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(dm); double x = math.pow(mwidthpixels/dm.xdpi,2); double y = math.pow(mheightpixels/dm.ydpi,2); double screeninches = math.sqrt(x+y); log.d("debug","screen inches : " + screeninches); 

credit him


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 -