DisplayMetrics metrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(metrics); screenWidth = metrics.widthPixels; screenHeight = metrics.heightPixels;
I use these codes to get the height and width of screen
the screen height on my Nexus7 should be 1280
but it return 1205...
and my minSdkVersion is level 8
so i can't use these method:
Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int screen_width = size.x; int screen_height = size.y;
now, how should i get the correct screen size ?
Edit:
if (Build.VERSION.SDK_INT >= 11) { Point size = new Point(); try { this.getWindowManager().getDefaultDisplay().getRealSize(size); screenWidth = size.x; screenHeight = size.y; } catch (NoSuchMethodError e) { Log.i("error", "it can't work"); } } else { DisplayMetrics metrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(metrics); screenWidth = metrics.widthPixels; screenHeight = metrics.heightPixels; }
use it works!
int height = displayMetrics. heightPixels; int width = displayMetrics.
Use window. innerWidth and window. innerHeight to get the current screen size of a page.
How to Get Width and Height of Android Screen in Pixels with Java Android ? Display display = getWindowManager(). getDefaultDisplay(); int width = display.
I've written a method that will get as close as possible for API 10 through 17+. It should give the real width and height for API 14+, and it does as well as it can for everything else.
Display display = context.getWindowManager().getDefaultDisplay(); int realWidth; int realHeight; if (Build.VERSION.SDK_INT >= 17){ //new pleasant way to get real metrics DisplayMetrics realMetrics = new DisplayMetrics(); display.getRealMetrics(realMetrics); realWidth = realMetrics.widthPixels; realHeight = realMetrics.heightPixels; } else if (Build.VERSION.SDK_INT >= 14) { //reflection for this weird in-between time try { Method mGetRawH = Display.class.getMethod("getRawHeight"); Method mGetRawW = Display.class.getMethod("getRawWidth"); realWidth = (Integer) mGetRawW.invoke(display); realHeight = (Integer) mGetRawH.invoke(display); } catch (Exception e) { //this may not be 100% accurate, but it's all we've got realWidth = display.getWidth(); realHeight = display.getHeight(); Log.e("Display Info", "Couldn't use reflection to get the real display metrics."); } } else { //This should be close, as lower API devices should not have window navigation bars realWidth = display.getWidth(); realHeight = display.getHeight(); }
I think this will work. The trick is you must use:
display.getRealSize(size);
not
display.getSize(size);
To deal with your API 8 coding issue do something like this:
try { display.getRealSize(size); height = size.y; } catch (NoSuchMethodError e) { height = display.getHeight(); }
Only more recent API devices will have onscreen navigation buttons and thus need the new method, older devices will throw an exception but will not have onscreen navigation thus the older method is fine.
In case it needs to be said: Just because you have a minimumAPI level of 8 for your project doesn't mean you have to compile it at that level. I also use level 8 for minimum but my project mostly are compiled at level 13 (3.2) giving them access to a lot of new methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With