I'm trying to display the screen width and height to logcat.
I have seen this stack overflow post: Getting screen width on API Level 30 (Android 11): getDefaultDisplay() and getMetrics() are now deprecated. What should we use instead?
But the accepted solution is confusing and complicated for someone like me new to DisplayMetrics and Display in Android Studio.
Currently, I know how to do it this way:
public class ScreenUtility {
    private Activity activity;
    private Float dpHeight, dpWidth;
    public ScreenUtility(Activity activity) {
        this.activity = activity;
        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        float density = activity.getResources().getDisplayMetrics().density;
        dpHeight = displayMetrics.heightPixels / density;
        dpHeight = displayMetrics.widthPixels / density;
    }
    public Float getDpHeight() {
        return dpHeight;
    }
    public Float getDpWidth() {
        return dpWidth;
    }
}
However, .getMetrics is deprecated. As a result, the code returns the width and heigh as null.
- (I am watching an older video on these concepts)Is there any way I can change this code and adjust my knowledge to the modern methods of - Displayand- DisplayMetrics?
- How are - DisplayMetricsand- Displaydifferent? In the docs, it describes them as both tools that provide the size of a Display. Or is- DisplayMetricsused as a parameter for- Display?
Thanks!!
 
     
    