Finding aspect ratio of an android device - android

In my application the UI does not look good for the newly introduced android devices with aspect ratio 4:3. In order to provide a better UI for such tablets I need to detect the aspect ratio of the device at runtime and need to make necessary UI adjustments.
How do I detect if the android device has an aspect ratio of 4:3?
I tried this for a Nexus 9 device (which has an aspect ratio of 4:3).
Used the following code:
DisplayMetrics widthMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(widthMetrics);
int width = widthMetrics.widthPixels;
DisplayMetrics heightMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(heightMetrics);
int height = heightMetrics.heightPixels;
float aspectRatio = (float) width/height;
Nexus 9 has a resolution 1536 X 2048. In the above code I receive the width value as 2048. But the height value 1440 (instead of 1536). Due to this, the aspect ratio I receive is 1.4222223 and not the expected 4/3(1.3333).
How do I resolve this issue?

Nexus 9 uses software buttons (home, back, ...). This height of those buttons will be substracted from the displayHeight.
The height is 48p in portrait mode, 42dp in landscape mode. So you are missing 96 Pixel in height: 48dp * 2 (xhdpi screen Nexus 9)
More information:
software buttons height

Related

How to scale layout proportionally to screen's physical size?

Need the whole layout width to fit exactly the same within the portrait physical width from the screen in any device, so that it doesn't matter the DPI, the screen size, the pixel portrait width, nor the width-height relation.
All I have managed to do right now is to override the font size from the user's Android configuration, by adding the next code to MainActivity.cs:
public override Resources Resources
{
get
{
Configuration config = base.Resources.Configuration;
if (config == null)
{
config = new Configuration();
}
config.FontScale = 1f;
return CreateConfigurationContext(config).Resources;
}
}
That made a great improvement, but still see plenty of difference between different devices, those devices may have longer or shorter screens, greater or smaller DPI, and different sizes. For example device Pixel 2 from emulator, which has 1080x1920 resolution and 420 dpi, the layout fits perfectly (I made it that way), but when I try in physical device Redmi 9 1080x2340 resolution and 395 dpi (about 3 mm bigger portrait width), the layout becomes a little bigger, and elements don't fit in.
How to make the layout width to scale proportionally to the physical size, so that it can always fit within the width from portrait mode no matter the device?
You can try using percentage or ratio dimension attributes of ConstraintLayout.
ConstraintLayout : ConstraintLayout

How to set minimum screen aspect ratio which app is supported in Google Play?

The app should not support the devices with aspect ratio less than 1.7
How to set minimum screen aspect ratio which app is supported in Google Play (API 19+)?
UPDATE: The necessity of these restrictions is not discussed.
The question is how to implement it (if it's possible)?
You can get the DisplayMetrics and calculate aspect ratio, and from code level show unsupported device message after launching the app.
To calculate aspect ratio, use below code:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float aspectRatio = (float)metrics.heightPixels / (float)metrics.widthPixels;
In your Application.onCreate(), if aspectRatio < 1.7, then show error layout.
Refer http://screensiz.es/phone for multiple devices height and width to calculate aspect ratio.

Android DisplayMetrics display absolute size varies with orientation

Has anyone noticed this issue and resolved the way of getting the absolute display size consistently in both orientations?
Example, Nexus 5 (5.0.1):
Portrait: width = 1080 height = 1776
Landscape: width = 1794 height = 1080
I would have thought that the height in portrait would match the width in landscape. Initially suspected the status bar, but docs are clear. Anyway the status bar height in this example is 75px in either orientation and the diff in the example is 18px.
Code snippet for display width and height in pixels:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
String dimensions = String.format("width = %d height = %d", width, height);
Log.v(TAG, dimensions);
Assuming the display is FullHD acording to your logging there's something of 144px (48dp) height in portrait and 126px (42dp) width in landscape occupying the display (when scaling factor is 3 which is xxhdpi). I bet it's the navigation bar with Back, Home and Recent buttons. This is sufficient for choosing layouts.
EDIT:
If you need the full display size the following code is available from API 17:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else {
display.getSize(size); // correct for devices with hardware navigation buttons
}
EDIT2:
If you want to make sure you get correct result on API 14 through 16 consider following this answer along with its comments
https://stackoverflow.com/a/11004877/2444099 .
It's not the entire screen size, its the size of the UI that display metrics gives you. You have to factor in the amount of screen size taken up by the area taken up by the status bar (clock, battery level, signal strength bar) which will differ on orientation.

Get the real screen size of android devices

I would like to use screen width/height as a basis to handle "size" issues in different devices.
I tried the following code, however, it can only get the "resolution" but not "screen size".
DisplayMetrics displayMetrics= getResources().getDisplayMetrics();
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
For example, my 7" tablet returns 600 width and my 4.3" phone returns 540 width.
It's unreasonable since tablet's size is almost a double of phone.
How can I get the real size under human vision or I should go for other approach to handle size issue?

Bitmap size difference for devices with same density, inches and resolution

I have two tablet devices:
1) Asus Memo Pad 172V tablet
Specs given online as: 600 * 1024 pixels, 7.0 inches (~170 ppi pixel density) LINK
Specs through code: 1024 * 552 pixels, 7.0 inches (160 ppi pixel density)
2) MID 7510 tablet
Specs given online as: 800 * 480 pixels, 7.0 inches (no density mentioned anywhere) LINK
Specs through code: 1024 * 552 pixels, 7.0 inches (160 ppi pixel density)
My problem is:
- Both the tablets have the same density and resolutions (by code), so how can I distinguish between them in order to set the bitmap height (width is coming correct for both) as in case of MID7510, the bitmap height is extended slight down.
- Is there any other factors that are responsible for causing different bitmap sizes for both
tablets ?
- Why are specs coming different by code and are given different online ?
Code to get density and resolution:
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
// Display device dpi (density) value in pixels
int screenDPIy = (int)dm.ydpi;
I'll try to answer to each question but I'll start by the more important one:
- Why are specs coming different by code and are given different online?
Because you find online what material is used for the device, its usually the correct values. You find by code specifications that are set up by the constructor when they are building their android version for their devices. Meaning : it can be false! especially for devices from unknown constructor or Chinese low cost such as 'weisung'.
Given that, answers for the other questions:
- Is there any other factors that are responsible for causing different bitmap sizes for both
tablets?
I dont think so, they have got different screen size and density but system think they are the same... it explain the difference on the final bitmap size.
- How can I distinguish between them?
Definitively not from the screen specifications. If you really need to manage 'weisung' devices you can always check for manufacturer in the device configuration by code. (Assuming they have correctly set up their identification in the manufacturer field XD)
Check the constant value : android.os.Build.MANUFACTURER
I'm really interested to know more in this topic too so any more information is welcome.
The chart was on drawable-hdpi folder.
I was doing simply this:
img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
The height was not proper in MID 7510 doing so.
Setting height like this worked for both the tablets:
static int imgHeight = 0;
Drawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.chart);
imgHeight = d.getIntrinsicHeight();
final ImageView img = new ImageView(getActivity());
img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) (imgHeight * 1.5)));
This is because 1 hdpi= 1.5 mdpi. So total height should be 1.5 times of image height.

Categories

Resources