Get screen width and height in Android

前端 未结 30 4055
野的像风
野的像风 2020-11-22 09:28

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         


        
30条回答
  •  耶瑟儿~
    2020-11-22 10:25

    Screen resolution is total no of pixel in screen. Following program will extract the screen resolution of the device. It will print screen width and height. Those values are in pixel.

    public static Point getScreenResolution(Context context) {
    // get window managers
    WindowManager manager =  (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    Point point = new Point();
    display.getSize(point);
    
     // get width and height
     int width = point.x;
     int height = point.y;
    
     return point;
    

    }

提交回复
热议问题