get dimension of displayable area of android device - android

I actually have two code snippets that I need to compare. Which one is more accurate? I am looking for the dimension of the drawable surface of a device.
Code 1:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int result[] = { metrics.heightPixels, metrics.widthPixels };
Code 2:
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
I need one for using inside a class that extends View

getSize() was introduced in API level 13. If you want to support API levels before that you should use your first option. In a view you can use it like:
DisplayMetrics metrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);‌​
int result[] = { metrics.heightPixels, metrics.widthPixels };
Otherwise There is no difference in accuracy

Related

AdMob Adaptive Banners. Get DisplayMetrics from Context

I want to use the newly introduced Google AdMob Adaptive Banners in my app. The quickstart illustrates a function to dynamically calculate the AdSize by DisplayMetrics.
private AdSize getAdSize() {
// Step 2 - Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float widthPixels = outMetrics.widthPixels;
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
// Step 3 - Get adaptive ad size and return for setting on the ad view.
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
But this function uses getWindowManager() of Activity for calculation. But I just pass Context to my Class. Anyway to get the DisplayMetrics with Context?
Easily accomplished.
This bit
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
can be replaced with this:
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
If are you using Cordova can use this:
this.cordova.getActivity().getWindowManager().getDefaultDisplay();

Dynamically change editTexts position based on screen Size / DPI

I would like to change the position of one element (editText) in my UI when the user taps it. The positioning should be ~20 dp under the actionBar, no matter screen-size or DPI the device is using.
How do I accomplish this using only (java)code? Please try to give a more exact answer. I have seen other examples similar to this but none that explains an exakt scenario? I was thinking of a start like the code below? Then what?:
DisplayMetrics dm = new DisplayMetrics();
mSearchField = (EditText)v.findViewById (R.id.something);
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int [] loc = new int[2];
mSearchField.getLocationOnScreen(loc);
int distance = dm.heightPixels - loc[1];
Get the Display height and width using DisplayMetrics.
DisplayMetrics displayMetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Log.e("width",width+""+height);

Screen size in API level 10 and forward?

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.

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();

Categories

Resources