how to get total screen size on an android device programmatically

后端 未结 8 1586
旧巷少年郎
旧巷少年郎 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 12:08
    int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;
    
    switch(screenSize) {
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
        break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
        break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
        break;
    default:
        Toast.makeText(this, "Screen size is neither large, normal or small" ,  Toast.LENGTH_LONG).show();
    }
    
    0 讨论(0)
  • 2021-01-02 12:11
    try this code...
    
    Display display = context.getWindowManager()
                .getDefaultDisplay();
            int width = display.getWidth();
        int height=display.getHeight();
    
    
    it will give screen's width and height,And according to width and height write if-    conditions for each device's screen resolution. 
    
    0 讨论(0)
提交回复
热议问题