How can I get the screen width and height and use this value in:
@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
Log.e(TAG, \"onM
As an android official document said for the default display use Context#getDisplay() because this method was deprecated in API level 30.
getWindowManager().
getDefaultDisplay().getMetrics(displayMetrics);
This bowl of code help to determine width and height.
public static int getWidth(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = context.getDisplay();
if (display != null) {
display.getRealMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
return -1;
}
For the Height:
public static int getHeight(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = context.getDisplay();
if (display != null) {
display.getRealMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
return -1;
}
int getScreenSize() {
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
// String toastMsg = "Screen size is neither large, normal or small";
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getRotation();
int i = 0;
switch (screenSize) {
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
i = 1;
// toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
i = 1;
// toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
// toastMsg = "Large screen";
if (orientation == Surface.ROTATION_90
|| orientation == Surface.ROTATION_270) {
// TODO: add logic for landscape mode here
i = 2;
} else {
i = 1;
}
break;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
if (orientation == Surface.ROTATION_90
|| orientation == Surface.ROTATION_270) {
// TODO: add logic for landscape mode here
i = 4;
} else {
i = 3;
}
break;
}
// customeToast(toastMsg);
return i;
}
For kotlin user's
fun Activity.displayMetrics(): DisplayMetrics {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
return displayMetrics
}
And in Activity you could use it like
resources.displayMetrics.let { displayMetrics ->
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
}
Or in fragment
activity?.displayMetrics()?.run {
val height = heightPixels
val width = widthPixels
}
As an android official document said for the default display use Context#getDisplay() because this method was deprecated in API level 30.
getWindowManager().
getDefaultDisplay().getMetrics(displayMetrics);
This code given below is in kotlin and is written accodring to the latest version of Android help you determine width and height:
fun getWidth(context: Context): Int {
var width:Int = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val displayMetrics = DisplayMetrics()
val display: Display? = context.getDisplay()
display!!.getRealMetrics(displayMetrics)
return displayMetrics.widthPixels
}else{
val displayMetrics = DisplayMetrics()
this.windowManager.defaultDisplay.getMetrics(displayMetrics)
width = displayMetrics.widthPixels
return width
}
}
fun getHeight(context: Context): Int {
var height: Int = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val displayMetrics = DisplayMetrics()
val display = context.display
display!!.getRealMetrics(displayMetrics)
return displayMetrics.heightPixels
}else {
val displayMetrics = DisplayMetrics()
this.windowManager.defaultDisplay.getMetrics(displayMetrics)
height = displayMetrics.heightPixels
return height
}
}
This set of utilities to work with the Size abstraction in Android.
It contains a class SizeFromDisplay.java You can use it like this:
ISize size = new SizeFromDisplay(getWindowManager().getDefaultDisplay());
size.width();
size.hight();
Methods shown here are deprecated/outdated but this is still working.Require API 13
check it out
Display disp= getWindowManager().getDefaultDisplay();
Point dimensions = new Point();
disp.getSize(size);
int width = size.x;
int height = size.y;