This question already has answers here:
How to get screen dimensions as pixels in Android
(46 answers)
Closed 10 years ago.
I would like to get the height of a android screen and if the screen inst a certain height, how would i go about doing this?
If you want the display dimensions in pixels you can use this code:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Then you can add condition that compares the height to satisfy your needs.
In inches:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screenInches);
From within activity:
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
Or if you only have Context object:
WindowManager windowManager = (WindowManager)mContext.getSystemService(WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight()
UPDATED. How to detect your application runs on large screen:
//Android Level 9 and up:
Configuration config = getResources().getConfiguration();
if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE)
{
//xlarge screen
}
In your onCreate or any other Activity method simply do:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Related
I have this piece of code :
Point point = new Point();
getWindowManager().getDefaultDisplay().getSize(point);
setScreenSize(point);
This returns the total height of the display in pixels.
However, this includes the notification bar and, on some devices, the bottom bar that contains the android-specific buttons (back, home and that other one).
My question is a two-parter.
How can I find out the height of the notification bar?
How can I find if the device has those buttons on the screen, and if it does, what is the height of that bar?
You can try these.I recently used below code in one of my projects. It works
Display display = getWindowManager().getDefaultDisplay();
String displayName = display.getName(); // minSdkVersion=17+
Log.i(TAG, "displayName = " + displayName);
// display size in pixels
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.i(TAG, "width = " + width);
Log.i(TAG, "height = " + height);
// get in (pixels or dpi)
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int heightPixels = metrics.heightPixels;
int widthPixels = metrics.widthPixels;
int densityDpi = metrics.densityDpi;
// this one is deprecated
int screenHeight = display.getHeight();
int screenWidth = display.getWidth();
Log.i(TAG, "screenHeight = " + screenHeight);
Log.i(TAG, "screenWidth = " + screenWidth);
You can find a lot of the default Android specifications such as StatusBar (Metrics & Keylines) and NavBar (Structure) in the Android Material Design Specification...
I believe that status bar is 24dp and NavBar is 48dp as standard.
You can get the width and height in DP by using:
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
float screenWidthDP = displayMetrics.widthPixels / displayMetrics.density;
float screenHeightDP = displayMetrics.heightPixels / displayMetrics.density;
Material Design Specification
You can do this way:
Display mDisplay = getWindowManager().getDefaultDisplay();
Point mPoint = new Point();
mDisplay.getSize(mPoint);
int width = mPoint.x;
int height = mPoint.y;
Hope this will help you.
I use this code to find the Height and width of the screen
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
But I get 1080 x 1920 for samsung galaxy S4 and 800 x 1280 for Nexus 7. But I actually need the the orginal heigth and width. How to get it.
I Hope this may help
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);
If you mean the size without subtracting room for the status bars and other decorations- use Display.getRealSize().
Try this way:
final DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = null,mGetRawW = null;
int realWidth=0,realHeight=0;
// For JellyBeans and onward
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
display.getRealMetrics(metrics);
realWidth = metrics.widthPixels;
realHeight = metrics.heightPixels;
} else{
// Below Jellybeans you can use reflection method
mGetRawH = Display.class.getMethod("getRawHeight");
mGetRawW = Display.class.getMethod("getRawWidth");
realWidth = (Integer) mGetRawW.invoke(display);
realHeight = (Integer) mGetRawH.invoke(display);
}
System.out.print(realWidth);
System.out.print(realHeight);
Normalize the pixel values using densityDpi value. This will give dimensions in terms of DP (density independent pixels).
int width = (displayMetrics.widthPixels * 160)/displayMetrics.densityDpi;
int height = (displayMetrics.heightPixels * 160)/displayMetrics.densityDpi;
Yes, I know I can use DisplayMetrics but the problem is that on some devices the widthPixels member is a wrong value (at least i have such one phone). The resolution in setting page is 720*1280, but widthPixels is 600 and heightPixels in DisplayMetrics is 1067.
Additionally, I have tried getRealSize and the returned value is correct. But eventually I found that the "real" width of my phone is 640 (and I guess the height is also a different number).
Awesome! what's really going on here?
Edit:
here're some data
widthPixels in DisplayMetrics is 600. density is 1.6, heightPixels is 1067.
And i have tried all your methods, but all not working. I won't need to post my question here if I could find the correct answer so easily. The device is HN3-U01, made in china
you can use this code..
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Try with this
private DisplayMetrics metrics;
private int widthPixels;
private float scaleFactor;
private float widthDp;
metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density= getResources().getDisplayMetrics().densityDpi;
widthPixels = metrics.widthPixels;
scaleFactor = metrics.density;
widthDp = widthPixels / scaleFactor;
// Get device resolution
int width = 0;
int height = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= 13)
{
w.getDefaultDisplay().getSize(size);
width = size.x;
height = size.y;
}
else
{
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
}
because getWidth() and getHeight() method deprecated after API level 13
if i use the code shown here to get the total screen size it is always short on height to the total screen size as shown in the documented specs for the device. for example I tried to get the screen size by using the code to get the size for a tablet listed as 1280 X 800 and the result from the code is: 1216 X 800. so where is the missing 64 pixels going to?
i can guess that it could be the bar at the bottom of the screen that holds the back and home buttons. but that does not sound right as the content views is supposed to be the root of all views. what is going on here?
the only possible explanation could be this part of the UI
code used to get screen size
// gets the content view windows width and height size
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int widthContentView = displaymetrics.widthPixels;
int heightContentView = displaymetrics.heightPixels;
Toast.makeText(MainActivity.this, "width of content view: " + widthContentView Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "height of content view: " + heightContentView, Toast.LENGTH_SHORT).show();
If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
I'm not sure what api you use but if you use api starting from 17 you can use this:
display.getRealSize();
try this code...
Display display = context.getWindowManager()
.getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
it will give screen's width and height,And according to width and height write if- conditions for each device's screen resolution.
You are right, It is showing you the resolution of your app screen and hence you getting the difference.Instead of getMetrics(), use getRealMetrics().
You can use the following code to get the actual screen size of device.
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
return width + "*" + height;
int density;
private DisplayMetrics metrics;
private int widthPixels;
private float scaleFactor;
private float widthDp;
metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
density= getResources().getDisplayMetrics().densityDpi;
widthPixels = metrics.widthPixels;
scaleFactor = metrics.density;
widthDp = widthPixels / scaleFactor;
System.out.println("widthDp== "+widthDp );
if(density==DisplayMetrics.DENSITY_HIGH)
System.out.println("Density is high");
if(density==DisplayMetrics.DENSITY_XXHIGH)
System.out.println("Density is xxhigh");
if(density==DisplayMetrics.DENSITY_XXXHIGH)
System.out.println("Density is xxxhigh");
if(density==DisplayMetrics.DENSITY_TV)
System.out.println("Density is Tv");
int Measuredwidth = 0;
int Measuredheight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
Measuredwidth = size.x;
Measuredheight = size.y;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();;
}
After searching for hours through stackOverFlow, couldn't find a single answer that was up to date so thought I would share mine. Hope this helps. :)
Method 1 (Deprecated)
fun Resolution() : String {
var width : Int? = 0
var height : Int? = 0
try {
val displayMetrics = DisplayMetrics()
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
width = displayMetrics.widthPixels
height = displayMetrics.heightPixels
}catch (e : Exception){
e.printStackTrace()
Log.e("DisplayClass", e.message, e)
}
return "$width x $height pixels"
}
Difference between getRealMetrics(displayMetrics) and getMetrics(displayMetrics) is that getMetrics() will return height/width without adding the size of status bars and navigation bars
windowManager.defaultDisplay.getRealMetrics(displayMetrics) // e.g: 1080 x 2340
windowManager.defaultDisplay.getMetrics(displayMetrics) // e.g: 1080 x 2260
Method 2 (Deprecated) (Minimum API : 30 (R))
This method returns the same result as getMetrics()
#RequiresApi(Build.VERSION_CODES.R)
fun Resolution() : String {
val width = context.display?.width
val height = context.display?.height
return "$width x $height"
}
Method 3 (Working as of 24th August 2022)
fun Resolution() : String {
var width: Int? = 0
var height : Int? = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = windowManager.currentWindowMetrics
width = metrics.bounds.width()
height = metrics.bounds.height()
}else{
val displayMetrics = DisplayMetrics()
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
width = displayMetrics.widthPixels
height = displayMetrics.heightPixels
}
return "$width x $height"
}
Get all Display set your width and height as you required.
Display display;
display=getWindowManager().getDefaultDisplay();
text1.setWidth(display.getWidth()-380);
text1.setHeight(display.getHeight()-720);
I am trying to get the screen size on my emulator (API 7) with this code:
float scaledDensity = this.getResources().getDisplayMetrics().scaledDensity;
Now I try to use scaledDensity
with this code:
int width = (width / ((int) scaledDensity)) / 7;
and I get this exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tomer.workoutlog2/com.example.workoutlog.SimpleCalendarViewActivity}: java.lang.ArithmeticException: divide by zero
Full code:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
int width = 0;
if (android.os.Build.VERSION.SDK_INT >= 13) {
Point size = new Point();
display.getSize(size);
width = size.x;
} else
width = display.getWidth();
float scaledDensity = this.getResources().getDisplayMetrics().scaledDensity;
width = (width / ((int) scaledDensity)) / 7;
On my real device I don't get this error.
Maybe, is the problem with the emulator?
for API 7 you can use the following code, it is deprecated from API 13
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
scaledDensity() returns a float. Sure you are not accidentally doing a integer division with an float less than 1.0f?
Try this...
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Try this code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
float scaleFactor = displaymetrics.density;