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!
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.
You can find real device dimensions with either using Display or DisplayMetrics
Using Display:
private void findRealSize(Activity activity)
{
Point size = new Point();
Display display = activity.getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT >= 17)
display.getRealSize(size);
else
display.getSize(size);
int realWidth = size.x;
int realHeight = size.y;
Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}
Using DisplayMetrics:
private void findRealSize(Activity activity)
{
DisplayMetrics displayMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= 17)
{
activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
}
else
{
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
}
int realWidth = displayMetrics.widthPixels;
int realHeight = displayMetrics.heightPixels;
Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}
Related
My phone is HUAWEI NXT-AL10, and I can see its resolution is 1080 x 1920 from the system settings page, But when I try to writing some codes as below to retrieving its screen resolution, the value is 1080 x 1794, I guess maybe it doesn't include the size of soft-keyboard. So How can I get the resolution just like as the settings page said (1080 x 1920)?
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels;
int height = metric.heightPixels;
The code below should give you what you're looking for
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(size);
iWidth = size.x;
iHeight = size.y;
Log.i(TAG, "Screen real size (pixels) :width = " + iWidth);
Log.i(TAG, "Screen real size (pixels) :height = " + iHeight);
Note that this requires API 17 and above
Thanks for #SimonH answer, and I add more logic to support API 16 and below, here they are just for your reference:
private void initScreenSize() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Point size = new Point();
getWindowManager().getDefaultDisplay().getRealSize(size);
mScreenWidth = size.x;
mScreenHeight = size.y;
} else {
Display display = getWindowManager().getDefaultDisplay();
try {
Method getHeight = Display.class.getMethod("getRawHeight");
Method getWidth = Display.class.getMethod("getRawWidth");
mScreenWidth = (Integer) getHeight.invoke(display);
mScreenHeight = (Integer) getWidth.invoke(display);
} catch (Exception e) {
mScreenWidth = display.getWidth();
mScreenHeight = display.getHeight();
}
}
}
I have this piece of code :
Point point = new Point();
getWindowManager().getDefaultDisplay().getSize(point);
setScreenSize(point);
This returns the total height of the display in pixels.
However, this includes the notification bar and, on some devices, the bottom bar that contains the android-specific buttons (back, home and that other one).
My question is a two-parter.
How can I find out the height of the notification bar?
How can I find if the device has those buttons on the screen, and if it does, what is the height of that bar?
You can try these.I recently used below code in one of my projects. It works
Display display = getWindowManager().getDefaultDisplay();
String displayName = display.getName(); // minSdkVersion=17+
Log.i(TAG, "displayName = " + displayName);
// display size in pixels
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.i(TAG, "width = " + width);
Log.i(TAG, "height = " + height);
// get in (pixels or dpi)
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int heightPixels = metrics.heightPixels;
int widthPixels = metrics.widthPixels;
int densityDpi = metrics.densityDpi;
// this one is deprecated
int screenHeight = display.getHeight();
int screenWidth = display.getWidth();
Log.i(TAG, "screenHeight = " + screenHeight);
Log.i(TAG, "screenWidth = " + screenWidth);
You can find a lot of the default Android specifications such as StatusBar (Metrics & Keylines) and NavBar (Structure) in the Android Material Design Specification...
I believe that status bar is 24dp and NavBar is 48dp as standard.
You can get the width and height in DP by using:
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
float screenWidthDP = displayMetrics.widthPixels / displayMetrics.density;
float screenHeightDP = displayMetrics.heightPixels / displayMetrics.density;
Material Design Specification
You can do this way:
Display mDisplay = getWindowManager().getDefaultDisplay();
Point mPoint = new Point();
mDisplay.getSize(mPoint);
int width = mPoint.x;
int height = mPoint.y;
Hope this will help you.
Since API 17 it is possible to get the actual screen size of a phone with:
if (android.os.Build.VERSION.SDK_INT >= 17){
display.getRealSize(size);
int screen_width = size.x;
screen_height = size.y;
} else {...}
I want to get the real screen size for APIs 8-16. What is the best way to handle the else condition in this case?
The following is my solution for getting the actual screen height on all APIs. When the device has a physical navigation bar, dm.heightPixels returns the actual height. When the device has a software navigation bar, it returns the total height minus the bar. I have only tested on a few devices but this has worked so far.
int navBarHeight = 0;
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navBarHeight = resources.getDimensionPixelSize(resourceId);
}
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
boolean hasPhysicalHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if (android.os.Build.VERSION.SDK_INT >= 17){
display.getRealSize(size);
int screen_width = size.x;
screen_height = size.y;
} else if (hasPhysicalHomeKey){
screen_height = dm.heightPixels;
} else {
screen_height = dm.heightPixels + navBarHeight;
}
Try this code:
display.getRealSize();
More about visited this link:
http://developer.android.com/reference/android/view/Display.html
or
You look at url:
https://stackoverflow.com/a/1016941/3821823
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
else{
display.getWidth()
display.getHeight()
}
edit: alternative solution
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.widthPixels
metrics.heightPixels
from the docs:
The absolute width of the display in pixels.
if even after those method the device insist in not giving it's real size, I don't believe there will be much you can do without editing the build.prop file on the device.
I use this code to find the Height and width of the screen
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
But I get 1080 x 1920 for samsung galaxy S4 and 800 x 1280 for Nexus 7. But I actually need the the orginal heigth and width. How to get it.
I Hope this may help
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);
If you mean the size without subtracting room for the status bars and other decorations- use Display.getRealSize().
Try this way:
final DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = null,mGetRawW = null;
int realWidth=0,realHeight=0;
// For JellyBeans and onward
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
display.getRealMetrics(metrics);
realWidth = metrics.widthPixels;
realHeight = metrics.heightPixels;
} else{
// Below Jellybeans you can use reflection method
mGetRawH = Display.class.getMethod("getRawHeight");
mGetRawW = Display.class.getMethod("getRawWidth");
realWidth = (Integer) mGetRawW.invoke(display);
realHeight = (Integer) mGetRawH.invoke(display);
}
System.out.print(realWidth);
System.out.print(realHeight);
Normalize the pixel values using densityDpi value. This will give dimensions in terms of DP (density independent pixels).
int width = (displayMetrics.widthPixels * 160)/displayMetrics.densityDpi;
int height = (displayMetrics.heightPixels * 160)/displayMetrics.densityDpi;
if i use the code shown here to get the total screen size it is always short on height to the total screen size as shown in the documented specs for the device. for example I tried to get the screen size by using the code to get the size for a tablet listed as 1280 X 800 and the result from the code is: 1216 X 800. so where is the missing 64 pixels going to?
i can guess that it could be the bar at the bottom of the screen that holds the back and home buttons. but that does not sound right as the content views is supposed to be the root of all views. what is going on here?
the only possible explanation could be this part of the UI
code used to get screen size
// gets the content view windows width and height size
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int widthContentView = displaymetrics.widthPixels;
int heightContentView = displaymetrics.heightPixels;
Toast.makeText(MainActivity.this, "width of content view: " + widthContentView Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "height of content view: " + heightContentView, Toast.LENGTH_SHORT).show();
If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
I'm not sure what api you use but if you use api starting from 17 you can use this:
display.getRealSize();
try this code...
Display display = context.getWindowManager()
.getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
it will give screen's width and height,And according to width and height write if- conditions for each device's screen resolution.
You are right, It is showing you the resolution of your app screen and hence you getting the difference.Instead of getMetrics(), use getRealMetrics().
You can use the following code to get the actual screen size of device.
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
return width + "*" + height;
int density;
private DisplayMetrics metrics;
private int widthPixels;
private float scaleFactor;
private float widthDp;
metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
density= getResources().getDisplayMetrics().densityDpi;
widthPixels = metrics.widthPixels;
scaleFactor = metrics.density;
widthDp = widthPixels / scaleFactor;
System.out.println("widthDp== "+widthDp );
if(density==DisplayMetrics.DENSITY_HIGH)
System.out.println("Density is high");
if(density==DisplayMetrics.DENSITY_XXHIGH)
System.out.println("Density is xxhigh");
if(density==DisplayMetrics.DENSITY_XXXHIGH)
System.out.println("Density is xxxhigh");
if(density==DisplayMetrics.DENSITY_TV)
System.out.println("Density is Tv");
int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();;
}
After searching for hours through stackOverFlow, couldn't find a single answer that was up to date so thought I would share mine. Hope this helps. :)
Method 1 (Deprecated)
fun Resolution() : String {
var width : Int? = 0
var height : Int? = 0
try {
val displayMetrics = DisplayMetrics()
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
width = displayMetrics.widthPixels
height = displayMetrics.heightPixels
}catch (e : Exception){
e.printStackTrace()
Log.e("DisplayClass", e.message, e)
}
return "$width x $height pixels"
}
Difference between getRealMetrics(displayMetrics) and getMetrics(displayMetrics) is that getMetrics() will return height/width without adding the size of status bars and navigation bars
windowManager.defaultDisplay.getRealMetrics(displayMetrics) // e.g: 1080 x 2340
windowManager.defaultDisplay.getMetrics(displayMetrics) // e.g: 1080 x 2260
Method 2 (Deprecated) (Minimum API : 30 (R))
This method returns the same result as getMetrics()
#RequiresApi(Build.VERSION_CODES.R)
fun Resolution() : String {
val width = context.display?.width
val height = context.display?.height
return "$width x $height"
}
Method 3 (Working as of 24th August 2022)
fun Resolution() : String {
var width: Int? = 0
var height : Int? = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = windowManager.currentWindowMetrics
width = metrics.bounds.width()
height = metrics.bounds.height()
}else{
val displayMetrics = DisplayMetrics()
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
width = displayMetrics.widthPixels
height = displayMetrics.heightPixels
}
return "$width x $height"
}
Get all Display set your width and height as you required.
Display display;
display=getWindowManager().getDefaultDisplay();
text1.setWidth(display.getWidth()-380);
text1.setHeight(display.getHeight()-720);