how to get total screen size on an android device programmatically

后端 未结 8 1585
旧巷少年郎
旧巷少年郎 2021-01-02 11:42

if i use the code shown here to get the total screen size it is always short on height to the total screen size as shown in the documented specs for the device. for example

相关标签:
8条回答
  • 2021-01-02 11:52
        int Measuredwidth = 0;
        int Measuredheight = 0;
        Point size = new Point();
        WindowManager w = getWindowManager();
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            w.getDefaultDisplay().getSize(size);
            Measuredwidth = size.x;
            Measuredheight = size.y;
        } else {
            Display d = w.getDefaultDisplay();
            Measuredwidth = d.getWidth();
            Measuredheight   = d.getHeight();;
        }
    
    0 讨论(0)
  • 2021-01-02 11:54
    int density;
        private DisplayMetrics metrics;
        private int widthPixels;
        private float scaleFactor;
        private float widthDp;
    metrics = new DisplayMetrics();
                    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
                    density= getResources().getDisplayMetrics().densityDpi;
    
                    widthPixels = metrics.widthPixels;
    
                    scaleFactor = metrics.density;
    
                    widthDp = widthPixels / scaleFactor;
    System.out.println("widthDp== "+widthDp );
    if(density==DisplayMetrics.DENSITY_HIGH)
                            System.out.println("Density is high");
    
                        if(density==DisplayMetrics.DENSITY_XXHIGH)
                            System.out.println("Density is xxhigh");
    
                        if(density==DisplayMetrics.DENSITY_XXXHIGH)
                            System.out.println("Density is xxxhigh");
    
                        if(density==DisplayMetrics.DENSITY_TV)
                            System.out.println("Density is Tv");
    
    0 讨论(0)
  • 2021-01-02 11:54

    Get all Display set your width and height as you required.

        Display display;
        display=getWindowManager().getDefaultDisplay();
        text1.setWidth(display.getWidth()-380);
        text1.setHeight(display.getHeight()-720);
    
    0 讨论(0)
  • 2021-01-02 11:59

    You are right, It is showing you the resolution of your app screen and hence you getting the difference.Instead of getMetrics(), use getRealMetrics(). You can use the following code to get the actual screen size of device.

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
    
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
    
        return width + "*" + height;
    
    0 讨论(0)
  • 2021-01-02 12:07

    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;
    
    0 讨论(0)
  • 2021-01-02 12:07

    I'm not sure what api you use but if you use api starting from 17 you can use this:

    display.getRealSize();
    
    0 讨论(0)
提交回复
热议问题