Get screen width and height in Android

前端 未结 30 3644
野的像风
野的像风 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 09:58

    As an android official document said for the default display use Context#getDisplay() because this method was deprecated in API level 30.

    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    This bowl of code help to determine width and height.

    public static int getWidth(Context context) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        Display display = context.getDisplay();
        if (display != null) {
            display.getRealMetrics(displayMetrics);
            return displayMetrics.widthPixels;
        }
        return -1;
    }
    

    For the Height:

    public static int getHeight(Context context) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        Display display = context.getDisplay();
        if (display != null) {
            display.getRealMetrics(displayMetrics);
            return displayMetrics.heightPixels;
        }
        return -1;
    }
    
    0 讨论(0)
  • 2020-11-22 09:58
        int getScreenSize() {
            int screenSize = getResources().getConfiguration().screenLayout &
                    Configuration.SCREENLAYOUT_SIZE_MASK;
    //        String toastMsg = "Screen size is neither large, normal or small";
            Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
            int orientation = display.getRotation();
    
            int i = 0;
            switch (screenSize) {
    
                case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                    i = 1;
    //                toastMsg = "Normal screen";
                    break;
                case Configuration.SCREENLAYOUT_SIZE_SMALL:
                    i = 1;
    //                toastMsg = "Normal screen";
                    break;
                case Configuration.SCREENLAYOUT_SIZE_LARGE:
    //                toastMsg = "Large screen";
                    if (orientation == Surface.ROTATION_90
                            || orientation == Surface.ROTATION_270) {
                        // TODO: add logic for landscape mode here
                        i = 2;
                    } else {
                        i = 1;
                    }
    
    
                    break;
                case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                    if (orientation == Surface.ROTATION_90
                            || orientation == Surface.ROTATION_270) {
                        // TODO: add logic for landscape mode here
                        i = 4;
                    } else {
                        i = 3;
                    }
    
                    break;
    
    
            }
    //        customeToast(toastMsg);
            return i;
        }
    
    0 讨论(0)
  • 2020-11-22 10:02

    For kotlin user's

    fun Activity.displayMetrics(): DisplayMetrics {
       val displayMetrics = DisplayMetrics()
       windowManager.defaultDisplay.getMetrics(displayMetrics)
       return displayMetrics
    }
    

    And in Activity you could use it like

         resources.displayMetrics.let { displayMetrics ->
            val height = displayMetrics.heightPixels
            val width = displayMetrics.widthPixels
        }
    

    Or in fragment

        activity?.displayMetrics()?.run {
            val height = heightPixels
            val width = widthPixels
        }
    
    0 讨论(0)
  • 2020-11-22 10:02

    As an android official document said for the default display use Context#getDisplay() because this method was deprecated in API level 30.

    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    This code given below is in kotlin and is written accodring to the latest version of Android help you determine width and height:

    fun getWidth(context: Context): Int {
        var width:Int = 0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val displayMetrics = DisplayMetrics()
            val display: Display? = context.getDisplay()
            display!!.getRealMetrics(displayMetrics)
            return displayMetrics.widthPixels
        }else{
            val displayMetrics = DisplayMetrics()
            this.windowManager.defaultDisplay.getMetrics(displayMetrics)
            width = displayMetrics.widthPixels
            return width
        }
    }
    
    fun getHeight(context: Context): Int {
        var height: Int = 0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val displayMetrics = DisplayMetrics()
            val display = context.display
            display!!.getRealMetrics(displayMetrics)
            return displayMetrics.heightPixels
        }else {
            val displayMetrics = DisplayMetrics()
            this.windowManager.defaultDisplay.getMetrics(displayMetrics)
            height = displayMetrics.heightPixels
            return height
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:02

    This set of utilities to work with the Size abstraction in Android.

    It contains a class SizeFromDisplay.java You can use it like this:

    ISize size = new SizeFromDisplay(getWindowManager().getDefaultDisplay());
    size.width();
    size.hight();
    
    0 讨论(0)
  • 2020-11-22 10:04

    Methods shown here are deprecated/outdated but this is still working.Require API 13

    check it out

    Display disp= getWindowManager().getDefaultDisplay();
    Point dimensions = new Point();
    disp.getSize(size);
    int width = size.x;
    int height = size.y;
    
    0 讨论(0)
提交回复
热议问题