How to get screen width and height

后端 未结 14 1028
梦如初夏
梦如初夏 2020-12-12 20:31

I tried to use following code to get screen width and height in android app development:

Display display = getWindowManager().getDefaultDisplay(); 
int scree         


        
相关标签:
14条回答
  • 2020-12-12 21:08

    below answer support api level below and above 10

     if (Build.VERSION.SDK_INT >= 11) {
                Point size = new Point();
                try {
                    this.getWindowManager().getDefaultDisplay().getRealSize(size);
                    screenWidth = size.x;
                    screenHeight = size.y;
                } catch (NoSuchMethodError e) {
                     screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
                     screenWidth=this.getWindowManager().getDefaultDisplay().getWidth();
                }
    
            } else {
                DisplayMetrics metrics = new DisplayMetrics();
                this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
                screenWidth = metrics.widthPixels;
                screenHeight = metrics.heightPixels;
            }
    
    0 讨论(0)
  • 2020-12-12 21:09

    Within an activity, you can call:

    int width = this.getResources().getDisplayMetrics().widthPixels;
    int height = this.getResources().getDisplayMetrics().heightPixels;
    

    When you're in a View, then you need to get the Context first:

    int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
    int height = getApplicationContext().getResources().getDisplayMetrics().heightPixels;
    

    This will work in all Android version, available since API 1, and never deprecated.

    0 讨论(0)
  • 2020-12-12 21:10
    if (Build.VERSION.SDK_INT >= 11) {
            Point size = new Point();
            try {
                this.getWindowManager().getDefaultDisplay().getRealSize(size);
                screenWidth = size.x;
                screenHeight = size.y;
            } catch (NoSuchMethodError e) {
                 screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
                 screenWidth=this.getWindowManager().getDefaultDisplay().getWidth();
            }
    
        } else {
            DisplayMetrics metrics = new DisplayMetrics();
            this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            screenWidth = metrics.widthPixels;
            screenHeight = metrics.heightPixels;
        }
    
    0 讨论(0)
  • 2020-12-12 21:16

    Try via context.getResources().getDisplayMetrics() if you have a context available.

    0 讨论(0)
  • 2020-12-12 21:17

    If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    

    UPDATE: With API level 17+, you can use getRealSize:

    Point displaySize = new Point();
    activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
    

    If you want the available window size, you can use getDecorView to calculate the available area by subtracting the decor view size from the real display size:

    Point displaySize = new Point();
    activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
    
    Rect windowSize = new Rect();
    ctivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowSize);
    
    int width = displaySize.x - Math.abs(windowSize.width());
    int height = displaySize.y - Math.abs(windowSize.height());
    return new Point(width, height);
    

    getRealMetrics may also work (requires API level 17+), but I haven't tried it yet:

    DisplayMetrics metrics = new DisplayMetrics();
    activity.GetWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    
    0 讨论(0)
  • 2020-12-12 21:17

    Try with the following code to get width and height of screen

    int widthOfscreen =0;
    int heightOfScreen = 0;
    DisplayMetrics dm = new DisplayMetrics();
            try {
                ((Activity) context).getWindowManager().getDefaultDisplay()
                        .getMetrics(dm);
            } catch (Exception ex) {
            }
             widthOfscreen  = dm.widthPixels;
    heightOfScreen  = dm.heightPixels;
    
    0 讨论(0)
提交回复
热议问题