Java – Android: How to find the width and height of the screen?

Android: How to find the width and height of the screen?… here is a solution to the problem.

Android: How to find the width and height of the screen?

I tried

to find the width and height of the screen, but all the methods I tried didn’t work for the class I created, my code is below.

Does anyone know how to find it? I can’t use the way I tried below because .getWidth() is deprecated?

public class Crate {

public int acrossCrate;
    public int upDownCrate;
    Context theContext;

WindowManager wm = (WindowManager)theContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int width = display.getWidth();

public Crate() {
    acrossCrate = 650;
    upDownCrate = 650;
    int width = ;
    int height = ;

}
 } 

Solution

Use the following code

private int getScreenHeight(Context context) {
        int height;

if (android.os.Build.VERSION.SDK_INT >= 13) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            height = size.y;
        } else {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            height = display.getHeight();   deprecated
        }

return height;
    }

Related Problems and Solutions