Is It Safe To Use .getwidth On Display Even Though Its Deprecated
So i have a small problem, i'm writing a function which need to send screen width to server. I got it all to work, and i use: Display display = getWindowManager().getDefaultDisplay
Solution 1:
May be this approach will be helpful:
DisplayMetricsdisplaymetrics=newDisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
intscreenWidth= displaymetrics.widthPixels;
intscreenHeight= displaymetrics.heightPixels;
Solution 2:
You can check for API level at runtime, and choose which to use, e.g.:
finalintversion= android.os.Build.VERSION.SDK_INT;
finalint width;
if (version >= 13)
{
Pointsize=newPoint();
display.getSize(size);
width = size.x;
}
else
{
Displaydisplay= getWindowManager().getDefaultDisplay();
width = display.getWidth();
}
Solution 3:
If you want to be correct, use this approach:
intsdk= android.os.Build.VERSION.SDK_INT;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
Displaydisplay= getWindowManager().getDefaultDisplay();
intwidth= display.getWidth();
} else {
Pointsize=newPoint();
display.getSize(size);
}
Solution 4:
Here is the latest Code without Deprecation.
Kotlin
val metrics: DisplayMetrics = this.getResources().getDisplayMetrics()
val width = metrics.widthPixels
Java
DisplayMetricsmetrics= context.getResources().getDisplayMetrics();
intwidth= metrics.widthPixels;
intheight= metrics.heightPixels;
Post a Comment for "Is It Safe To Use .getwidth On Display Even Though Its Deprecated"