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?
Related
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
I tried this doing with device dpi. Getting dpi and scaling image accordingly but does not work on all devices. I have written code in cocos2dx framework and tried it on android, devices like xolo and xiomi my note does not display the desired result (image size different from rest devices).
My concept is if one device has 320 dpi and another has 240 dpi than
1 inch = 320 px(in 1st device) = 240 px(in 2nd device)
for android I have used
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.xdpi
metrics.ydpi
and scaled accordingly for each phone
Is it possible that all devices not return correct dpi.
Is this a correct way of doing it?
Is there an alternate solution for obtaining this result?
For some devices xdpi and ydpi metrics are returning wrong numbers and you cannot rely on them. Since API 17 you can also use getRealMetrics(). It should give you accurate values for xdpi and ydpi.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(dm);
dm.xdpi
dm.ydpi
I have a android tablet( Samsung gt-P1010 ) whose screen details is as follows,
Screen resolution- 600*1024
Density- 240
Screen size- Normal
I created an emulator of the same details like above but when I print the Width and Height, it gives 600*961 .Why there is such a big difference ? I am using the following code to get the device details,
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
In my application I am trying to run a .gif image above the Alphabets for which I am taking margins based on the width and height of the device. Due to the above difference, emulator and real device gives different result. Please anyone tell me whats wrong here.Any help will be appreciated.
Display metrics give the screen dimensions including the status bar and excluding the soft keys area. On 2.x devices it will give you the complete screen size but on 4.x devices(which have soft keys) it will exclude the soft key area.
I am making an app for a tablet which has a resolution of 1280 x 800, and I want custome graphics for it. But when i took a screenshot of the screen the resolution was 1024x600, why is this? What size background image would I make?!
As mentionned here : http://developer.android.com/reference/android/util/DisplayMetrics.html
if you want the pixel of your screen, you can write
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
and then, you can call :
int width = metrics.widthPixels;
int height = metrics.heightPixels;
in the simulator it is actually resized to fit your screen and takes in your screens resolutions nothing wrong with your code or images.
Screen shot dimension maynot be same as resolution.
you need to make graphics with 1280 x 800.
screen shot taken depend on theway how it is created,it doesnt have to be same resolution as display.
Is there an API in Android that returns the full screen height of the device? I'm interested in the full height as specified in the device spec, not the height of the viewable screen as returned by
android.view.Display.getHeight();
I spent some time looking for this and didn't find anything like what you're asking for. Part of the problem is that the bar that takes up the pixels they're not including can potentially be of different sizes.
What I ended up doing is measuring the width of the device (which is the full width) and the not-quite-full-height to match against the standard resolutions with an approximate. It's not great. If you really need it exact, you can force an orientation change to match both dimensions exactly. That's pretty gross, though.
The real answer is that you're not supposed to care about the exact dimensions of the hardware and design using the OS-provided size buckets.
Does this not work for you?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayHeight = dm.heightPixels;
For me, displayHeight returns 960 pixels on my HTC Sensation
Try heightPixels on a DisplayMetrics object.