Get screen width and height in Android

前端 未结 30 3646
野的像风
野的像风 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:21

    Just use the function below that returns width and height of the screen size as an array of integers

    private int[] getScreenSIze(){
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int h = displaymetrics.heightPixels;
            int w = displaymetrics.widthPixels;
    
            int[] size={w,h};
            return size;
    
        }
    

    On your onCreate function or button click add the following code to output the screen sizes as shown below

     int[] screenSize= getScreenSIze();
            int width=screenSize[0];
            int height=screenSize[1];
            screenSizes.setText("Phone Screen sizes \n\n  width = "+width+" \n Height = "+height);
    
    0 讨论(0)
  • 2020-11-22 10:23

    None of the answers here work correctly for Chrome OS multiple displays, or soon-to-come Foldables.

    When looking for the current configuration, always use the configuration from your current activity in getResources().getConfiguration(). Do not use the configuration from your background activity or the one from the system resource. The background activity does not have a size, and the system's configuration may contain multiple windows with conflicting sizes and orientations, so no usable data can be extracted.

    So the answer is

    val config = context.getResources().getConfiguration()
    val (screenWidthPx, screenHeightPx) = config.screenWidthDp.dp to config.screenHeightDp.dp
    
    0 讨论(0)
  • 2020-11-22 10:23

    I updated answer for Kotlin language!

    For Kotlin: You should call Window Manager and get metrics. After that easy way.

    val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)
    
    var width = displayMetrics.widthPixels
    var height = displayMetrics.heightPixels
    

    How can we use it effectively in independent activity way with Kotlin language?

    Here, I created a method in general Kotlin class. You can use it in all activities.

    private val T_GET_SCREEN_WIDTH:String = "screen_width"
    private val T_GET_SCREEN_HEIGHT:String = "screen_height"
    
    private fun getDeviceSizes(activity:Activity, whichSize:String):Int{
    
        val displayMetrics = DisplayMetrics()
        activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
    
        return when (whichSize){
            T_GET_SCREEN_WIDTH -> displayMetrics.widthPixels
            T_GET_SCREEN_HEIGHT -> displayMetrics.heightPixels
            else -> 0 // Error
        }
    }
    

    Contact: @canerkaseler

    0 讨论(0)
  • 2020-11-22 10:24

    Just to update the answer by parag and SpK to align with current SDK backward compatibility from deprecated methods:

    int Measuredwidth = 0;  
    int Measuredheight = 0;  
    Point size = new Point();
    WindowManager w = getWindowManager();
    
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
        w.getDefaultDisplay().getSize(size);
        Measuredwidth = size.x;
        Measuredheight = size.y; 
    }else{
        Display d = w.getDefaultDisplay(); 
        Measuredwidth = d.getWidth(); 
        Measuredheight = d.getHeight(); 
    }
    
    0 讨论(0)
  • 2020-11-22 10:25

    Why not

    DisplayMetrics displaymetrics = getResources().getDisplayMetrics();

    then use

    displayMetrics.widthPixels (heightPixels)

    0 讨论(0)
  • 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;
    

    }

    0 讨论(0)
提交回复
热议问题