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.
Related
This may end up being a "yes" or "no" question...
Starting in Nougat (maybe marshmallow?), users can change the display size of their device. Specifically on the Galaxy S8 and S8+, the user can change the display resolution.
I've seen the solutions here: Get Screen width and height
QUESTION: Do these solutions give the actual height/width of the screen regardless of changed resolution or do they give the adjusted height/width of the screen if resolution changed?
If the first, how do I get the adjusted height/width?
Interesting question. It gives me adjusted dimensions in pixels. I have tested it now in my Pixel running Android O.
Method 1:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Method 2:
Configuration configuration = getResources().getConfiguration();
int screenWidthDp = configuration.screenWidthDp;
int screenHeightDp = configuration.screenHeightDp;
int smallestScreenWidthDp = configuration.smallestScreenWidthDp;
Both methods returned the adjusted dimensions. The first method returns the device screen dimensions. The second method returns the dimensions of the app screen. These two result would vary when there are two apps open in multi-window mode.
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();
I am trying to figure out the dimensions for the S4. What is the width and height. I want to create graphics and place them based on h and w. I read that the screen size ranges from 0-h and 0-w, but what are h and w?
S4 screen specs are 1080 x 1920, but you shouldn't use the actual screen size in pixels in your code.
Just use dp - which is a size which is independent in the screen density to design you Android app UI, this way, it will behave nicely for all screen sizes.
For more details see Supporting Multiple Screens Guidelines.
Here's some handy code if you need to find the size of various devices:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
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
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;