简体   繁体   中英

How can I get the width and Height of the screen -Android

I tried using

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

metrics.heightPixels;
metrics.widthPixels;

but because I use View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY the width is no right. It shows the size without the navigation bar. but I want to know all the screen size..

Thanks!

This is only possible in API 17 and later. Just use getRealMetrics(metrics) instead of calling getMetrics() . (Or you can call getRealSize(Point) if all you need is the size.) For an approach to use with earlier API levels, see this thread .

Its working fine for me

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

Try

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

use this code this is implemented in API 13

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

or

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM