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();
Related
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);
How to reduce spinner size like this, reduce it's width & move it's alignment to right. Please refer below screenshots
What I want is below
What I get...
Set Width of spinner
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics(); display.getMetrics(outMetrics);
int width = outMetrics.widthPixels / 2;
popupWindow.setWidth(width);
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.
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
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();