Device width for an android phone in landscape mode? - android

I'm looking to adjust my page width when an android user rotates their phone to landscape. Problem is, I don't know what the device width is to set an exact pixel amount for my container div. I guess at 640, but it doesn't seem right. Does anyone know, offhand, what the device width for a landscape-oriented android device is? Or does that vary from device to device?
Thanks!

You shouldn't be guessing for an exact pixel width. I will just use dip (instead of px) on some of the glue pieces (like padding or the sides) and just let the main application expand naturally to fill it up.

It varies from device to device. You have to make generic ways to draw the screens. If you're setting xml layouts, then you can set widths and heights using the various notations.
What is the difference between "px", "dp", "dip" and "sp" on Android?
If you're setting the size dynamically at runtime, then screen size can be obtained through the "Display" class.
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Log.d(TAG, "Height = " + display.getHeight() + " Width = " + display.getWidth());
The width and height will be adjusted depending on your orientation of the phone.
The information is generally given to you through the onDraw methods through the Canvas object. You can use canvas.getWidth() to get the width of the screen and canvas.getHeight() to get the height of the screen.

Related

Scaling drawables not to density (dip), but actual pixel resolution (px)

I've been working on an app where it is important that drawables are always the same size relative to the display's width, e.g. always 1/8 of the screen height in pixels. I understand how density independent pixels work, but as their goal is to always represent the same physical size, it is pretty much the opposite of what i need. How can I achive this? I have tried using a .png-file with a very high resolution and downscale accordingly, but its always blurry and the quality loss is unbelievable.
Thank you for your help, best regards.
Had the exact same issue. Here's my solution:
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
Double newHeight = 0.125 * displayMetrics.heightPixels;
yourImageView.getLayoutParams().height = newHeight.intValue();
Alternatively, use a LinearLayout. Set your ImageView's weight to 1, and the LinearLayout's weightSum to 8.

variable screen resolution sprite not scaling unity

I am trying to make my 1st game in unity. I need to support different resolutions of android and iOS. The problem is when i place the background image for any one res ex. 1536x2048,It doesn't displays properly for 640x960. The screen size is varying, but sprites and bg sprite is not scaling as per res.
I am not clear how to acheive it. But, it must be a basic thing as there are so many games on android and all phones have different res. and dpi.
There should be some formula or we just need to set orthographic.size for this. I am using this :
// to calc size
Camera.main.orthographicSize = Screen.currentResolution.height / (2*70); //
Camera.main.orthographicSize = Screen.currentResolution.height / 2;
Debug.Log("Camera size :" + Camera.main.orthographicSize);
Debug.Log("Width : " + Screen.currentResolution.width + " height : " + Screen.currentResolution.height);
// this doesnt give me width and height as per resolution of screen for ex. for 1536x2048 -> i get, Width : 1366 height : 768
i found this as similar question
http://forum.unity3d.com/threads/orthographic-screen-size.113641/ but it didnt help, whereas problem is nearly same.
If somebody can share how to get correct screen resolution by code and how to set width of bg sprite and other sprite, it can solve the problem OR
if there is some setting which takes the default scene and scales automatically for various resolution. it will help.
Unity does not scale like that. Instead, we design assets to suit our needs. You are using a formula but i don't know what you expect from it.
here's some info: Orthographic size is height of view port. Taking your example, for height of 2048, camera size is : 2048/2/100=10.24. This will be maintained irrespective of device screen size. Only difference being it will be less detailed on smaller devices, kind of like a zoom out effect. Now for width,i'm not sure for portrait, but let's say 9:16 is the widest. So if you design the bg for this aspect ratio, the bg will be cropped on smaller devices, shown full on 9:16 devices. To design for this aspect ratio, your bg needs to be 9/16*10.24*100=576 px. (Again i'm not sure which aspect ratio is widest in portrait mode). Hope that helps

how can i know the actual size of an imageView in my device?

I have an ImageView with matchParent property in width.
how can i know its runtime width on my device (using eclipse, without adding code programmatically)?
how can I know the conversion ratio between dpi to pxls in my device?
I have an ImageView ... how can i know its runtime width on my device
You can get the width when the ImageView is measured, i.e. later than the runtime. However you can get the screen's width and height like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
And if for example your ImageView takes up a specific portion of your screen you can measure it using the screen's width and height.
how can I know the conversion ratio between dpi to pxls in my device?
To convert say 20 pixels to DP do (adding to the above code):
int dp = 20 / metrics.density:
Extra: Here's a dpi to pixel calculator
If you don't want to use the TreeObserverListener way, you can try to get the View to measure itself and give you what the dimensions would be if it were calculated. In my experience, this has worked in most cases but not all; especially if the View is in some special dynamic layout or the ordering of the hierarchy prevents the dimensions from being calculated in the correct order. You might find luck in refering to this popular answer.

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?

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