Why does screen width and screen change with changing orientation? - android

I ran an application on my nexus 7 that calculated the screen width and height on my device. In portrait mode, my application gave me a height of 1824 px and a width of 1200 px. These dimensions made sense to me. However in landscape mode, my application gave me a height of 1104px and a width of 1920 px. From Does the Width and Height change with orientation?, I got that with changing orientation, screen width and height change, but shouldnt the dimensions just flip? To me, its the same fundamental screen
The code i used to calculate the screen size
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight();
int width = display.getWidth();

Related

Finding aspect ratio of an android device

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

Android how to evaluate view size to keep same proportion in all devices

I'm searching for a way to keep view size proportioned on all devices. I experimented with many maths calculations (e.g. mixing screen size, density, percentage, etc.) but haven't found any solutions, just different results for each device tested.
Let me present an example: Suppose I'm testing an app on a sw-360dp and eventually I find a size view that works.
relativeLayoutParams.width = 150;
relativeLayoutParams.height = 250;
This has the effect of creating a view with size about 1/3 of screen width and 1/2 screen height.
How can I dynamically arrive at value for width and height, that lets the view keep the aspect ratio of 1/3 width to 1/2 height? At least I think that's what the documentation is suggesting when it says: "calculate exact view size (or margin, padding, etc.) from a percentage of screen size".
Or have I misinterpreted it?
Any help will be appreciated.
You can get the device screen size and there by define the ratio,
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Then,
width = (int) size.x * 1/3 ;
height = (int) size.y * 1/2 ;

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.

Change Orientation while converting android handset app to tablet

I am converting and android handset application to tablet (1024*600 and 1280*720). I have given the support for different screen- size, but stuck in orientation part. I want some pages to be visible as landscape. For example after logged in by user the next intent should be in landscape form. After searching I found clue but not very understandable answer such as
By adding activity in manifest.xml file
At runtime by using getOrientation().
Thanks
The question is not clear, if you're forcing screen orientation by size (e.g. on tablet device there's only landscape orientation), i think you should get the screen width and height first then evaluate it. If the screen width and height are 1024*600 or 1280*720, you change the orientation with setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
To get the width and height, do this in your activity:
Display display = getWindowManager().getDefaultDisplay();
int width, height;
try {
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
catch (NoSuchMethodError e) {
width = display.getWidth();
height = display.getHeight();
}
See this

auto resizing textView with different density devices android

At the top of my app, I have a title which should be shown in the middle, and a button on the right. As the textViews length is behind my control, I sometimes have my title crossing the button due to long length of the content of it.
After following this, I somehow tend to solve the problem. My device was HTC desire. Unfortunately, if I check with Galaxy SIII, it doesn't do the trick.
I am wondering how I can manage this in terms of different devices with different densities.
My controls inside the relative layout
You can also check the device screen density by this--
WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
And can manage accordingly whats your apps needed..
just use weightsum in your xml and make width of all the views as fill parent ..... this makes auto resizing of your textview
you can maintain layouts according to their DPI`s
replicate the same XML data in XHDPI (As S3 falls in XHDPI) and test it similarly replicate the XML data in HDPI
but
keep in mind the following Thing Pixel Ratio of the layOut as
following
in LDPI its 1:0.75
in MDPI its 1:1
in HDPI its 1:1.5
in XHDPI its 1:2
Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
float density = dm.density;
int screenWidth = display.getWidth();
With this code above, you'll have your screen density as float.. So you can use it to calculate your textView's width like:
int newWidth = (int) (density * 100);
which 100 is here based size.
Or you can have a ratio according to your screenWdith.
int newWidth = screenWidth / 2;

Categories

Resources