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.
Related
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.
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 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?
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.
In android there are many different types of screen Resolutions available. How to adjust the screen resolutions using java code?
There is no way to alter the screen resolution programmatically. AFAIK, you'd need to do a hardware level modification to change it.
Instead, you must write your app in a manner that it scales well across displays. Use multiple images for different sizes and densities, use dp units instead of px units. Make layouts for different screen sizes. All of this and more is explained in the online documentation here.
If you are asking about how to make your App adjust to the device resolution, you should read the screen resolution using code below and make your app fill the space as your wish.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
As there are 1000s of different Android devices, you just can't have a single line of code to do it automatically for you.
If you are speaking about changing the screen resolution like in PCs. LCD screens have native resolutions which cannot be altered. However, in PCs, the driver or the hardware behind the monitor change the resolution by considering consecutive pixels as single pixel. This option is added in high resolution monitors just to keep the compatibility with old video cards. In mobile phones, we do not have a need for doing this, so there is no such options. When LCD monitors replaced CRT, they had a need to work in different resolutions like CRT which can work on more than one native resolutions.
public int AR(int input)
{
final float scale = getResources().getDisplayMetrics().density;
return (int) (input * scale + 0.5f);
}
where, input is the value you want to use.
Example:
ImageView TitleImage = new ImageView(this);
TitleImage.setPadding(AR(10), AR(5), AR(2), AR(3));
NOW THE IMAGE PADDING SUITS ALL KINDS OF RESOLUTIONS.