Emulator's height and width differs from that of real device - android

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.

Related

Phonegap android get the actual screen size

I know there are a lot of questions similar to my title but I couldn't find the answer that I want.
In my app, I am using window.innerWidth and window.innerHeight to get the screen size, but these two values return different size. For example when I run my app on Note 3, I get 360x615 while it should be 1080x1920.
The following code in Java gives me what I want:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
So is there any way in Phonegap to get the same screen size value as Java?
Update: when I test the same code on Note 2 with Jelly Beans I get the correct size 720x1230
I am using this code
hope it help you
var physicalScreenWidth = window.screen.width * window.devicePixelRatio;
var physicalScreenHeight = window.screen.height * window.devicePixelRatio;
I am assuming you want to figure out the actual device pixels instead of the more commonly measured CSS pizels? You could read up on this here.
Basically, if you want to know actual device pixels you could use screen.width and screen.height. If you want to know CSS pixels you could use window.innerWidth and window.innerHeight.
The meta viewport tag may be ignored on certain Android devices, those are my experiences with Samsung Galaxy tab devices anyway.

(Android)Unable to get the correct screen width for some devices

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.

Get the real screen size of android devices

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?

android, get pixel size of screen?

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.

Get full display height of the device, specified in the device spec.

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.

Categories

Resources