display diagonal size conflict in various android device - android

I am working to get device display diagonal length. I used the formula, diagonal = width* width + height* height. To achieve this I use below code.
DisplayInfo aDisplayInfo = new DisplayInfo();
DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
aDisplayInfo.widthInch = (dm.widthPixels * dm.density) / (dm.xdpi * 2);
aDisplayInfo.heightInch = ((dm.heightPixels * dm.density) / (dm.ydpi * 2));
aDisplayInfo.widthPix = dm.widthPixels;
aDisplayInfo.heightPix = dm.heightPixels;
// approaach 1
aDisplayInfo.diagonalInch = twoDecimalForm.format(Math.sqrt(Math.pow(
aDisplayInfo.widthInch, 2)
+ Math.pow(aDisplayInfo.heightInch, 2)));
After running this code I found out different results in different devices. Like:
Samsung Galaxy S3 = 4.8 inches (correct)
HTC One X = 6.8 inches (wrong) [ will be 4.7 inches]
What's wrong with this code? Any kind of help will be appreciated.

Try this:
aDisplayInfo.widthInch = dm.widthPixels / dm.xdpi;
aDisplayInfo.heightInch = dm.heightPixels / dm.ydpi;
widthPixels/heightPixels should be the physical pixel count. xdpi/ydpi should be pixels per inch. You shouldn't need to do any scaling on these values.

Related

Android how to force screen density by default

By magnifying the screen by "Settings / Displays", I've found that my galaxy s9+'s screen density changes to 430 dpi to somewhere over 600dpi.
This makes the layout and images to change to xxhdpi to xxxhdpi.
If I have a textView which has the fixed size of 16dp,
it was 16 * 3 px in xxhdpi, and will become 16 * 4 px in xxxhdpi.
This makes my layout(of course texts from editTexts or textViews) to become much bigger since the actual density of the device never changes, which google(or the maker samsumg) intended for this function.
But I do not want this to happen in my app.
I've tried to fix it by doing this ;
Configuration configuration = activity.getResources().getConfiguration();
if (configuration.densityDpi != 430) {
configuration.densityDpi = 430;
}
DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) activity.getSystemService(activity.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.densityDpi * metrics.density;
activity.getResources().updateConfiguration(configuration, metrics);
This forces the screen density to become 430 and works fine on galaxy s9+.
But, as a matter of fact, the default density will not be 430 for all devices.
How can I figure out the default density of the device which runs my application?
Or is there a configuration to ignore the magnify effect for my application?
This seems to work for me.
DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
int snap = 20;
float exactDpi = (displayMetrics.xdpi + displayMetrics.ydpi) / 2;
float dpi = displayMetrics.densityDpi;
if (dpi - exactDpi > snap) {
int targetDpi = (int) (Math.ceil(exactDpi / snap) * snap);
Configuration config = activity.getResources().getConfiguration();
ErrorController.showMessage("adjustDisplayScale : " + config.densityDpi);
ErrorController.showMessage("targetDpi : " + targetDpi);
displayMetrics.densityDpi = targetDpi;
config.densityDpi = targetDpi;
displayMetrics.setTo(displayMetrics);
config.setTo(config);
activity.getResources().updateConfiguration(config, displayMetrics);
}

Code for Android Screen Size doesn't work

I have tried the codes below for finding out the screen size but none worked for me. The interface also goes out of proportion for different devices of the same screen sizes and same dimensions. I don't have actual devices, I am using the devices with screen sizes/dimensions provided by the AVD. The codes work for dimensions but it doesn't give the correct screen size. The screen size it calculates for a 4 inch screen is 3.9 inches (round figure) and the screen size for a 3.7 inch screen is also 3.9 inches. The interface code for the Nexus S (4", 480x800: hdpi) doesn't work for the 4" WVGA (480x800: hdpi) where both have the same screen size and same dimensions.
Could you please help me solve this 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 for example: 120 , 160 , 240
float mXDpi = metrics.xdpi; // 160 The exact physical pixels per inch of the screen in the X dimension.
float mYDpi = metrics.ydpi;
//density
int nDensity = metrics.densityDpi; // 160 screen density expressed as 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 a screen.
int wPix = metrics.widthPixels; // 320 The absolute width of the display in pixels.
int hPix = metrics.heightPixels; // 480 The absolute height of the 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 to get android device screen size?
Calculating android screen size?
Detect 7 inch and 10 inch tablet programmatically
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
final int height = metrics.heightPixels;
int width = metrics.widthPixels;

Android : ScreenSize Support for 8 and 7 inch screen

I have a problem which I`m unable to solve lately..
I have make a simple app for tablet which I run in my Samsung Galaxy Note 8.0 .. I have set the layout to place some text view at some place.. I code it in xml using dp for margin and sp for text size. The width and height layout is set to wrapcontent. But when I run it in my friend Samsung Galaxy Tab 2 7.0, the layout did not match what I see in my device. The same thing happen when I try to run it in 10inch emulator.
I have use layout-sw600dp for my layout folder..
Why my screen layout is like that?I though by using layout-sw600dp the layout supposedly to adjust itself.. Or am I wrong regarding that?
I have read android documentation regarding different screensize support and so far I found using layout-sw600dp is good solution..
I have checked the device dpi using :
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
Every device has different pixel ratio, different dpi, different screen size and so on. Go to this link, read it, you will know how to manage different device layouts.
Android - Supporting Multiple Screen
If you want to check the screen inch size when the app start, you can calculate the inch with the following two methods, but I think the second one is more precisely.
public static float getScreenInchSizeByDensityDpi(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
double x = Math.pow((double)dm.widthPixels / (double)dm.densityDpi, 2);
double y = Math.pow((double)dm.heightPixels / (double)dm.densityDpi, 2);
double screenInches = Math.sqrt(x + y);
return (float) screenInches;
}
public static float getScreenInchSizeByXYDpi(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
double x = Math.pow((double)dm.widthPixels / (double)dm.xdpi, 2);
double y = Math.pow((double)dm.heightPixels / (double)dm.ydpi, 2);
double screenInches = Math.sqrt(x + y);
return (float) screenInches;
}

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

smallest width layouts. bug in Nexus 7?

When using the layout-swdp qualifiers I get the results as shown in the attachment. The sw qualifier is supposed to mean the smallest dimension must match or be bigger than the qualifier. This doesn't seem to work with the Nexus 7 (running 4.2.1). Am I confused about what smallest width qualifiers do or is the N7 reporting wrongly?
To reproduce my test case, I have many layout-swdp folders. Each has 2 textfield. The first just states which folders it's in. The next is the code below:
private CharSequence collectScreenStats() {
StringBuilder str = new StringBuilder();
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
int dpWidth = (int)(width / metrics.density);
int dpHeight = (int)(height / metrics.density);
str.append(Build.MANUFACTURER);
str.append(" ");
str.append(Build.MODEL);
str.append("\n");
str.append("Pixels: ");
str.append(width);
str.append(" x " );
str.append(height);
str.append("\nDp (px / density): ");
str.append(dpWidth);
str.append("dp x " );
str.append(dpHeight);
str.append("dp" );
str.append("\nsmallest w: " + Math.min(dpWidth, dpHeight));
str.append("\ndensity: ");
str.append(metrics.density);
str.append("\ndensityDpi: ");
str.append(metrics.densityDpi);
return str;
}
Okay, this seems to be a bug in ICS where it doesn't accurate report the number of pixels of the entire screen as it taking into account the chrome.
Android DisplayMetrics returns incorrect screen size in pixels on ICS
So, my above display numbers are off as nexus 7 is 1280 x 800 and not 1280 x 736. Using the correct numbers, everything works.

Categories

Resources