I am trying to get the width and height of the screen in Android, i have:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
stageWidth = displaymetrics.widthPixels;
stageHeight = displaymetrics.heightPixels;
The width is 1280 but its coming back with 752.
Any ideas?
Try this instead:
Display display = getWindowManager().getDefaultDisplay();
stageWidth = display.getWidth();
stageHeight = display.getHeigth();
Is that the same width? Are you sure the width is 1280 and not 752? What device are you testing it on and what height are you getting?
Use this method on the display object (from the docs):
void getSize(Point outSize) -
Returns the raw size of the display, in pixels.
Rotate your screen and see.I think you will get your correct screen size.
The above code worked with me and should work with you.
Related
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.
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
screen_width = display.getWidth();
screen_height = display.getHeight();
I understand that the code above could drive me to get the screen width and height. I put this code in "onCreate" of "MainActivity".
But my friend's mobile device could not show the desired layout. Yet, it is too trouble for me to debug in his mobile device. Thus, I look forward to the help here.
My Question is ...
Is it right to get the width and height in "onCreate"?
Is it useful to get the width and height of all devices? (of course, API>=11)
Yes it's right to get the Screen Height and width in onCreate() method because if your Screen rotates then your onCreate will be called. and it's not mandatory that you should take Screen Height & width if needed OK or else you can manage it in Xml as well. To get Screen height and width i think this is the best way...
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
for more details you use this link as well
http://developer.android.com/reference/android/view/Display.html
You could try metrics instead:
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
int displayHeight = metrics.heightPixels;
int displayWidth = metrics.widthPixels;
Doing it in onCreate should be fine.
But honestly, I don't think you are really getting the wrong height and width. The screen size is probably just very different from your test devices.
You will need to debug with his phone at least once to find out what the problem is.
In my opinion it is a good approach to get the width and height, because it gives you more control. Most layout stuff could be done by working with proper layouts (eg LinearLayouts with weight), but there are cases where doing it manually will work better. You can do things more precisely, but it will be more work.
Also if you don't have a large variety of devices for testing it will be tough to adjust it properly. Just think about all the different sizes the Android world can confront you with. I for example had some problems with very small devices, like a Galaxy Mini. Also very big devices like tablets might be a problem. Theres just no way around testing edgecases if you want to do it manually.
I have 1dp line between each table row,I am able to see those lines in low resolution screen's. But if I see the same thing in nexus 4 those lines are not visible ,how can I make those 1dp lines visible in high resolution screens, so that my design will be same in all resolutions.?
Use the following code
Display display = getWindowManager().getDefaultDisplay();
int ScreenHeight = display.getHeight();
int ScreenWidth = display.getWidth();
View view = (View) findViewById(R.id.view);
LayoutParams paramsView = new LayoutParams((int)(ScreenWidth * .01), LayoutParams.FILL_PARENT);
view.setLayoutParams(paramsView);
It will set the width of your View to 1% of the ScreenWidth... You can increase the perecentage or use ScreenHeight variable as per your requirement!
I know it's been a while since the question, but there are several ways to do this:
1) Make a dimension entry in dimens.xml at 1dp:
<dimen name="oneDP">1dp</dimen>
2) Get the DisplayMetrics, and then multiply the number of pixels by density:
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float scaleFactor = metrics.density;
It's late, but this might help somebody in the future
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;
I am debugging my application on 2 different Droid devices: Bionic and Droid3. When I use WindowManager to display device width & height, it shows 540x960 (portrait) and 960x540 (landscape) for both devices! How is that possible? The 2 devices are clearly of a different size. Here is the code:
Display display = getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
final int height = display.getHeight();
Thanks,
Igor
I read that getWidth()/getHeight() is deprecated.
Try to use this:
Display display = getWindowManager().getDefaultDisplay();
Point displayDimensionInPixel = new Point();
display.getSize(displayDimensionInPixel);
I'm not shure right now because the Documentation isn't there anymore, but I think I can remember that getWidth()/getHeight() of Display dont return pixel dimensions.