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

I want to calculate device size by inch. I am using this code that every search appears. But the problem is when I put device 4.5 inch my answer in android studio is 4. I tried 5.2 inch device and I got 4.3 inch as well 10.1 inch -> the answer is 9.0. So how can I get 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 with your code is it does not take into account
Actionbar
Softkeys
Orientation of the screen
use the 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 to him

You can use getResources().getDisplayMetrics()
DisplayMetrics: A structure describing general information about a
display,
such as its size, density, and font scaling.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
int dens = metrics.densityDpi;
double wi = (double)width / (double)dens;
double hi = (double)height / (double)dens;
double x = Math.pow(wi, 2);
double y = Math.pow(hi, 2);
double screenInches = Math.sqrt(x+y);
System.out.println("getSIZE"+screenInches);
For more information visit Android DisplayMetrics returns incorrect screen size.

Device Size is different from screen size as screen will not cover total device size
Android OS has nothing to do with device size you cannot programatically get device dimensions you can get only screen dimensions

Try This With Just Small Change
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
int dens = metrics.densityDpi;
double wi = (double)width / (double)dens;
double hi = (double)height / (double)dens;
double x = Math.pow(wi, 2);
double y = Math.pow(hi, 2);
DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
try {
double screenInches = Double.parseDouble(df.format(Math.sqrt(x+y)));
Log.e("hello"," "+screenInches);
} catch (NumberFormatException e) {
e.printStackTrace();
}

Related

How to get the screen resolution including the size of soft keyboard?

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();
}
}
}

Calculate Android screen size

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.

How to get phone screens height and width

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;

How to get the Display Size in Inches in Android?

I need for my Application the exact inch of a display. My current solution is:
double inch;
double x = Math.pow(widthPix/dm.xdpi,2);
double y = Math.pow(heigthPix/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
inch = screenInches;
inch = inch * 10;
inch = Math.round(inch);
inch = inch / 10;
Is there a better way to get the display inch? Current on my test device 4 inch it say 5.9 and that is wrong...
The following gave me a a result quite close to the specs:
DisplayMetrics dm = getResources().getDisplayMetrics();
double density = dm.density * 160;
double x = Math.pow(dm.widthPixels / density, 2);
double y = Math.pow(dm.heightPixels / density, 2);
double screenInches = Math.sqrt(x + y);
log.info("inches: {}", screenInches);
Output: inches: 4.589389937671455
Specs: Samsung Galaxy Nexus (720 x 1280 px, ~320 dpi, 4.65")
Please note, that dm.heightPixels (or dm.widthPixels depending on your orientatation) does not necessarily provide accurate information, since soft keys (as used in the Galaxy Nexus) are not added to the height (or width).
Try this.... This will give you the exact size of the display..
static String getDisplaySize(Activity activity) {
double x = 0, y = 0;
int mWidthPixels, mHeightPixels;
try {
WindowManager windowManager = activity.getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
mWidthPixels = realSize.x;
mHeightPixels = realSize.y;
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
x = Math.pow(mWidthPixels / dm.xdpi, 2);
y = Math.pow(mHeightPixels / dm.ydpi, 2);
} catch (Exception ex) {
ex.printStackTrace();
}
return String.format(Locale.US, "%.2f", Math.sqrt(x + y));
}

How to get android device screen size?

I have two android device with same resolution
Device1 -> resolution 480x800 diagonal screen size -> 4.7 inches
Device2 -> resolution 480x800 diagonal screen size -> 4.0 inches
How to find device diagonal screen size?
Detect 7 inch and 10 inch tablet programmatically
I have used the above link but it gives both device diagonal screen size -> 5.8
try this code to get screen size in inch
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
double wi=(double)width/(double)dm.xdpi;
double hi=(double)height/(double)dm.ydpi;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
This won't work?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
double screenInches = Math.sqrt(x + y);
Log.d("debug", "Screen inches : " + screenInches);
Don't forget to multiply the screen size by the scaledDensity if you are doing what I did and change the size of stuff based on the screen size. e.g.:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
double screenInches = Math.sqrt(x + y) * dm.scaledDensity;
Log.d("debug", "Screen inches : " + screenInches);
Note the second last line! Here's more info the scaledDensity stuff: What does DisplayMetrics.scaledDensity actually return in Android?
None of the above answers gave correct screen size... But the below method does it right.
private String getScreenSize() {
Point point = new Point();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealSize(point);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int width=point.x;
int height=point.y;
double wi=(double)width/(double)displayMetrics.xdpi;
double hi=(double)height/(double)displayMetrics.ydpi;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
return String.valueOf(Math.round((Math.sqrt(x+y)) * 10.0) / 10.0);
}
Note: This works only on API 17 & above.
Try this:
public static Boolean isTablet(Context context) {
if ((context.getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
return true;
}
return false;
}
DisplayMetrics displayMetrics = context.getResources()
.getDisplayMetrics();
String screenWidthInPix = displayMetrics.widthPixels;
String screenheightInPix = displayMetrics.heightPixels;
Pythagoras theorem to find the diagonal size of Android phone/tablet screen, same principal can be applied to iPhone or Blackberry screen.
Try as below the other way:
DisplayMetrics met = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(met);// get display metrics object
String strSize =
new DecimalFormat("##.##").format(Math.sqrt(((met.widthPixels / met.xdpi) *
(met.widthPixels / met.xdpi)) +
((met.heightPixels / met.ydpi) * (met.heightPixels / met.ydpi))));
// using Dots per inches with width and height

Categories

Resources