Android WindowManager shows same screen size for different devices - android

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.

Related

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.

Android: Samsung S4 Screen size

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;

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;

Android Dev: max screen length

I am creating an application for android OS. The problem im facing is that running the application on different devices with different resolutions, the layout eitehr become too small, on hi res screens, or too chunky on low res screens. How do i make it so that my layouts adapt to the screen on the device??
Do i use relative layout Or is there a way i can just find out the max screen length and width...?
Use RelativeLayout -- thats the best option.
In anycase, if you want to find out the max dimensions:
private void setupGlobal() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Global.screenHeight = height;
Global.screenWidth = width;
}

Categories

Resources