In an Android Application, I want to know the height and the width of the screen. I'm using the following code:
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
The problem is than when the soft navigation bar exists, the returned height = the actual height - navigation bar height.
I want to get the total value.
Note: I'm using Android SDK 13 (v 3.2).
Check this, it may help:
FrameLayout root = (FrameLayout) findViewById(R.id.root);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the height of Status Bar
int statusBarHeight = rect.top;
// Get the height occupied by the decoration contents
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// Calculate titleBarHeight by deducting statusBarHeight from contentViewTop
int titleBarHeight = contentViewTop - statusBarHeight;
// By now we got the height of titleBar & statusBar , Now lets get the screen size
int screenHeight = dm.heightPixels;
// Now calculate the height that our layout can be set If you know that your application doesn't have statusBar added, then don't add here also.
//Same applies to application bar also
int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
}
});
You can use
display.getRealSize(size); instead of display.getSize(size);
source:
http://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
you can use DisplayMetrics. This class holds information about the screen such its size, density, font scaling. For instance
getResources().getDisplayMetrics().widthPixels
returns
The absolute width of the display in pixels.
to retrieve the height
getResources().getDisplayMetrics().heightPixels
In onCreate method of your class write down this code, and you will get the actual width and height of screen:
Display mDisplay = this.getWindowManager().getDefaultDisplay();
final int width = mDisplay.getWidth();
final int height = mDisplay.getHeight();
System.out.println("width " + width + " height " + height);
Related
I'm setting up a gridview of 7 columns. I want it to have 6 rows and fill the screen. So, am trying to find the height of the screen excluding actionbar, statusbar and softkeys. Am planning to set this height divided by 6 as the height of each row. For some reason, the height am getting is not accurate.
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int height = metrics.heightPixels;
This will give you correct screen dimensions
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
I have a toggle button that changes it's size as following :
protected int[] calculateCardWidth(int[] rowsCols) {
//Getting the screen size.
Display display = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//cardWidHigh 0->width , 1 ->height.
cardWidHigh[0] = (int) (width / (rowsCols[1] + .25));
cardWidHigh[1] = (int) (height / (rowsCols[0] + 2));
return cardWidHigh;
}
I want to be able to change the text size according to the change in button size (it's width and height )dynamically, what is the best approach inorder to achieve this ?
You change the values of its LayoutParams, the parent will use the new values to rerender the screen at new positions:
LayoutParams p = cardView.getLayoutParams();
p.width = cardWidth[0];
p.height = cardWidth[1];
cardView.setLayoutParams(p);
cardView.getViewParent().invalidate();
I want to get Android device's screen size. I'm using these 3 codes to do this but these are giving me the activity's size; which is without the size of the status bars height.
On tablet with 800px height, this code gives 764px. 36px goes to status bar on the bottom.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int height2= rectgle.bottom;
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int actualHeight = metrics.heightPixels;
But I need a code which gives me the actual size of the screen. Not the activity's. can anyone help me about this?
Thank you.
For API Level 17 and above you can use getRealSize()
https://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
This snippet will give you height of your status bar.
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
So the total height of screen is :-
height = height+TitleBarHeight;
original answer is here. Also note this will not work in onCreate put that code in runnable.
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();
Please get me correctly over here :
I want to get the height/width of the space available to the Activity/Layout in onCreate() method to calculate the height that can be given to child layouts. I can get the screen size using :
root = (LinearLayout) findViewById(R.id.mainroot); // Main layout of LinearLayout
android.view.Display display = getWindowManager().getDefaultDisplay();
int height = Display.getHeight(); // I know this is deprecated have hence used
int width = Display.getWidth(); // DisplayMetrics
int childWidth, childHeight;
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
//int density = metrics.densityDpi;
height = metrics.heightPixels; //480
width = metrics.widthPixels; //320
This both the methods/ways gives me same height and width i.e. size of full screen. What I am looking for is to get actual height that is avaialbe for the layout after deduction of Application Title, Status Bar, etc.
Any idea how to get this. OR to get the sizes of titles, etc - what all should be counted over here. On emulator I see 2 bars on top - 1 must be application titile what an be the other one. I can get heights of them all and deduct from screen height.
ONE more point : In this case I will be setting the height programamtically so it will be pixel based (as I can setheight in pixels only I guess) will that affect in density factor with differnet screen sizes. What can be a way to calculate height (lets say 50%) for child layout that will be same for any density o so.
SOLUTION :
In my onCreate(), I added the following lines :
setContentView(R.layout.mainpage);
root = (LinearLayout) findViewById(R.id.mainroot);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusHeight = rect.top;
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
titleHeight = contentViewTop - statusHeight;
Log.i(Utility.TAG, "titleHeight = " + titleHeight + " statusHeight = " + statusHeight + " contentViewTop = " + contentViewTop);
// CALCULATE THE SIZE OF INNER LAYOUTS
calculateChildSize();
}
});
With the above code, I get the values of titleBar & statusBar. On deducting it from metrics.heightPixels; I get the required height of the screen.
Good Point is this code works for all density's.
Hope this helps others too.
FOR IMPROVEMENT : I have to do similar calculations for all Activities in my application, so was thinking about writing this code only once. I can save teh titleHeight to a static variable so can use in all activities.
BUT
Can the user change the phone's density at runtime.
If so, then the Activity's onCreate will be called again or not ?
If not, then can I trap the density change event where I can add this code and make the current activity to refresh.
Any idea suggestions for improving is appreciated.
//Where rootView is the object for the root view of your application
final int viewWidt = rootView.getMeasuredWidth();
There is very easy method for that. doOnLayout() method is called as soon as layout is measured and ready:
rootView.doOnLayout {
rootView.getMeasuredWidth()
rootView.getMeasuredHeight()
}