Screen size in API level 10 and forward? - android

I want to get the screen size and I'm using a device that has API level 10. I'm using Display to get the width and height, but I'm getting a message in Eclipse that this is deprecated. A better and newer way is to use DisplayMetrics for apps that have a newer API, but there is no support for DisplayMetrics in API 10. How should I be doing to handle this issue?
Old(deprecated)
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
New
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
metrics.widthPixels;

Just check the API level like this:
int screenWidth = 0;
int screenHeight = 0;
if(android.os.Build.VERSION.SDK_INT < 10) {
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
} else {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
}
But DisplayMetrics was added in API level 1. So actually you only need the else-clause.

In my application I use getResources().getDisplayMetrics() directly on my activity object. It exactly gives me the screen size. It works fine as it is available from API level 1.

Related

android detect device width

is there a way to detect device width so that we can add views programmatically based on device screen width. For example, single or dual pane.
Right now, the way for adpative UI is using layout XML for different device size. This is not flexible enough in our case.
layout-small/
layout-large/
layout-sw600dp/
The preferred way for us is: layout.xml defines the root empty container only, and add views programmatically based on device size that is detected at runtime.
Thanks.
U can use this code to get runtime Device's display Width & Height
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
U can set like
view.setLayoutParams(new LayoutParams(width, height));
This code will support all version of android device Resolution(Width, Height) accurately at runtime
private void calculateDeviceResolution(Activity context) {
Display display = context.getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT >= 17) {
//new pleasant way to get real metrics
DisplayMetrics realMetrics = new DisplayMetrics();
display.getRealMetrics(realMetrics);
realWidth = realMetrics.widthPixels;
realHeight = realMetrics.heightPixels;
} else if (Build.VERSION.SDK_INT >= 14) {
//reflection for this weird in-between time
try {
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
realWidth = (Integer) mGetRawW.invoke(display);
realHeight = (Integer) mGetRawH.invoke(display);
} catch (Exception e) {
//this may not be 100% accurate, but it's all we've got
realWidth = display.getWidth();
realHeight = display.getHeight();
Constants.errorLog("Display Info", "Couldn't use reflection to get the real display metrics.");
}
} else {
//This should be close, as lower API devices should not have window navigation bars
realWidth = display.getWidth();
realHeight = display.getHeight();
}
}
after getting the width or height you can set like this
view.setLayoutParams(new LayoutParams(realWidth , realHeight ));
In an activity,
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
In a fragment,
DisplayMetrics dm = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;

Get screen resolution of device

I used following method to get the screen size:
public static Point getScreenSize(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int w = wm.getDefaultDisplay().getWidth();
int h = wm.getDefaultDisplay().getHeight();
return new Point(w, h);
}
On my samsung galaxy s6, this method returns 1080x1920... Although my device has a resolution of 1440x2560.
Why? Is there a better method to get the screen resolution that works on newer phones reliable as well?
EDIT
I need this method in a service! I don't have a view/activity for help, only the application context! And I need the REAL pixels
The best way to get your screen resolution is from DisplayMetrics :
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
If you are targeting for API >= 17, try with the getRealSize method of the Display class:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getRealSize(size);
String resolution = size.x + "x" + size.y;
for me, it worked.
Use this one:
private static String getScreenResolution(Context context)
{
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
return "{" + width + "," + height + "}";
}
Use this.
DisplayMetrics metrics;
int width = 0, height = 0;
In your onCreate method.
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height = metrics.heightPixels;
width = metrics.widthPixels;
The API returns the number of pixels an app can use, so this is the correct result.
Try this:
Display mDisplay = context.getWindowManager().getDefaultDisplay();
mDisplay.getWidth();
mDisplay.getHeight();
Note that this code uses deprecated APIs.

Is it safe to use .getWidth on Display even though its deprecated

So i have a small problem, i'm writing a function which need to send screen width to server. I got it all to work, and i use:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
to get width. However .getWidht() function is deprecated and it says u need to use:
Point size = new Point();
display.getSize(size);
But that function is only avaible for api level 13 or more, and my minimum sdk is 8. So what can i do? Is it safe if i stay with getWidth? Why adding new function and not make them backward compatible?
May be this approach will be helpful:
DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
You can check for API level at runtime, and choose which to use, e.g.:
final int version = android.os.Build.VERSION.SDK_INT;
final int width;
if (version >= 13)
{
Point size = new Point();
display.getSize(size);
width = size.x;
}
else
{
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
}
Here is the latest Code without Deprecation.
Kotlin
val metrics: DisplayMetrics = this.getResources().getDisplayMetrics()
val width = metrics.widthPixels
Java
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If you want to be correct, use this approach:
int sdk = android.os.Build.VERSION.SDK_INT;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
} else {
Point size = new Point();
display.getSize(size);
}

How to get screen resolution of your android device..? [duplicate]

This question already has answers here:
How to get screen dimensions as pixels in Android
(46 answers)
Closed 9 years ago.
Can i get resolution of android phone..?
if yes then how..?
it will really helpful for me..
Thank you..
If you want the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
This method was introduced in Android API 13, so if you want to get display metrics for previous APIs use:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
If you're not in an Activity, you can get the default Display via WINDOW_SERVICE. Also be sure to set your minSdkVersion to at least 4 in AndroidManifest.xml to enable calls to display.getWidth().
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Edit: PhoneGap
Use the following to figure out the display size in pixels
function getDeviceDimention() {
console.log("Device Dimention using PhoneGap");
console.log("Width = " + window.innerWidth);
console.log("Height = " + window.innerHeight);
}
For Cordova / Phonegap, I've found that using
var width = screen.width
var height = screen.height
is more reliable than using
var width = window.innerHeight
var height = window.innerWidth
in order to obtain the resolution of the current display. The latter is unfortunately inaccurate if your mark-up either overflows or does not take up all of the space available on the screen.
For phonegap you can use this
You can use device-width and device-height
<meta name="viewport" content="width=device-width; user-scalable=no" />
You can even get device height and width using jquery & jquery mobile like this
var width = $(window).width();
var height = $(window).height();
NOTE:- Do this when device is ready.
You can call
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point outSize = new Point();
display.getRealSize(outSize);
outSize.x is the width and outSize.y is the height. Hope it help.
like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
look at this answer Android: How to get screen dimensions
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Good luck!
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
good luck
행운을 빈다.
u can get screen resolution like this
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();

Android: Get the screen resolution / pixels as integer values

this is a really simple question on which I've found no answer :/
How can I quickly access the screen resolution (width, height) as integer values?
I've tried this one, but it always shows zero on my emulator:
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;
In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.
Can someone give me a hint?
The trashkalmar answer is correct.
However, the results will be specific to the activity context. If you want the whole device screen resolution:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
Note that the results also depend on the current device orientation.
You can get screen metrics in this way:
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
1.Simply use This inside activity to get screen width and height pixels.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
2.This can also be used But requires Api level inlined check
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
int width2 = size.x;
int height2 = size.y;
} else {
int width2 = display.getWidth();
int height2 = display.getHeight();
}
3.Use This when you are not in activity but having context
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();
int width1;
int height1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display1.getSize(size1);
width1 = size1.x;
height1 = size1.y;
} else {
width2 = display.getWidth();
height2 = display.getHeight();
}
I use the following:
int scrWidth = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();
No muss, no fuss, just 2 class variables
The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.

Categories

Resources