I want to change the screen width and height for whole platform. So I want to know where should I change ?
As I understand that DisplayMetrics provides the width and height for all apps. How DisplayMetrics gets those width and height ? Any leads is appreciated.
For getting : width and height :
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
i use this when i made a code to setup wallpaper :
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Related
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;
I know there are many answered questions about how to get screen width and height, but I tried all I could find and I can't get any of them to work.
For example, in this code I get error for context:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Use getWindowManager().getDefaultDisplay().get height()/get width().
Try to get Context of Application with getApplicationContext().
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If I extend activity in my app I can get width and height:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
or
Display display = getWindowManager().getDefaultDisplay();
stageWidth = display.getWidth();
stageHeight = display.getHeigth();
But at present I extend fragment and I can't use the above code to get the width.
Try below modifed code of yours
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
or
Display display = getActivity().getWindowManager().getDefaultDisplay();
int stageWidth = display.getWidth();
int stageHeight = display.getHeight();
Basically you just required to put getActivity() (to get the context) before getWindowManager() function.
This will give you what you want without needing for context or view:
import android.content.res.Resources;
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
This code also works with Fragments:
int width = getResources().getConfiguration().screenWidthDp;
int height = getResources().getConfiguration().screenHeightDp;
For comparing orientation:
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){...}
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();
I am trying to get the size of the screen. The size I have set for emulator is 540 x 960 but what I am getting is 320 x 480. Any suggestions on it to get the right size. Here is the code;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
Try this:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
try this one:
int height = getWindowManager().getDefaultDisplay().getHeight();
int width = getWindowManager().getDefaultDisplay().getWidth();