Wednesday 15 April 2015

Code for Android Screen Size doesn't work -



Code for Android Screen Size doesn't work -

i have tried codes below finding out screen size none worked me. interface goes out of proportion different devices of same screen sizes , same dimensions. don't have actual devices, using devices screen sizes/dimensions provided avd. codes work dimensions doesn't give right screen size. screen size calculates 4 inch screen 3.9 inches (round figure) , screen size 3.7 inch screen 3.9 inches. interface code nexus s (4", 480x800: hdpi) doesn't work 4" wvga (480x800: hdpi) both have same screen size , same dimensions.

could please help me solve problem.

displaymetrics dm = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(dm); int widthinch=dm.widthpixels; int heightinch=dm.heightpixels; int dens=dm.densitydpi; double wi=(double)widthinch/(double)dens; double hi=(double)heightinch/(double)dens; double x = math.pow(wi,2); double y = math.pow(hi,2); double screeninches = math.sqrt(x+y); screeninches = (double)math.round(screeninches * 10) / 10; int widthpix = (int) math.ceil(dm.widthpixels * (dm.densitydpi / 160.0)); toast.maketext(getapplicationcontext(), width+"*"+height+" - "+widthpix+ " - " +screeninches, toast.length_long).show(); //================================================================ displaymetrics metrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(metrics); //get density per inch example: 120 , 160 , 240 float mxdpi = metrics.xdpi; // 160 exact physical pixels per inch of screen in x dimension. float mydpi = metrics.ydpi; //density int ndensity = metrics.densitydpi; // 160 screen density expressed dots-per-inch float mmeterstopixelsx = mxdpi / 0.0254f; // 1 inch == 0.0254 metre float mmeterstopixelsy = mydpi / 0.0254f; //resolution //the total number of physical pixels on screen. int wpix = metrics.widthpixels; // 320 absolute width of display in pixels. int hpix = metrics.heightpixels; // 480 absolute height of display in pixels. int nwidthdisplay = (wpix < hpix)? wpix : hpix; float nwidthscreenininch = wpix / mxdpi; //320 / 160 == 2.0 in inch. float nheightscreenininch = hpix / mydpi; //480 / 160 == 3.0 in inch. double screeninches = math.sqrt( nheightscreenininch*nheightscreenininch + nwidthscreenininch * nwidthscreenininch); screeninches = (double)math.round(screeninches * 10) / 10; toast.maketext(getapplicationcontext(),"screen size: "+screeninches, toast.length_long).show(); //================================================================ displaymetrics metrics=new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(metrics); float hgt=metrics.heightpixels/metrics.xdpi; float wdth=metrics.widthpixels/metrics.ydpi; double screeninches = floatmath.sqrt(hgt*hgt+wdth*wdth); screeninches = (double)math.round(screeninches * 10) / 10; toast.maketext(getapplicationcontext(),width+"*"+height+" - "+screeninches, toast.length_long).show(); //===============================================================

how android device screen size?

calculating android screen size?

detect 7 inch , 10 inch tablet programmatically

displaymetrics metrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(metrics); final int height = metrics.heightpixels; int width = metrics.widthpixels;

android

No comments:

Post a Comment