0
votes

According to the Android documentation for screen support, they have described as follow,

  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp

I have a code snippet as follows;

Display display = getWindowManager().getDefaultDisplay();
int displayWidth = display.getWidth();

I need to get an idea for the value range for XML layouts which is related to displayWidth out-put.

As an example, I want an answer like;

if 0 < displayWidth < 320 are small screens

Above phrase may wrong, I just saw an example.

Further, we can have directories like layout, layout-small, layout-large, layout-xlarge and layout-xlarge-land under res directory. So what is the displayWidth applicable for those layouts.

As an example just like above;

if 0 < displayWidth < 320 for layout-small

Thank you.

3

3 Answers

0
votes

I think' it's better to calculate diagonal length of the screen as below.

DisplayMetrics  displayMetrics = getResources().getDisplayMetrics();
    double screen_phys_width  = (double)displayMetrics.widthPixels / displayMetrics.xdpi;
    double screen_phys_height = (double)displayMetrics.heightPixels / displayMetrics.ydpi; 
    screen_diagonal = Math.sqrt(screen_phys_width * screen_phys_width + screen_phys_height * screen_phys_height);
0
votes
  try {
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        if (display.getHeight() == 480 && display.getWidth() == 320) {

                  //Medium Screens
            i = 1;
            return i;
        } else if (display.getHeight() == 320 && display.getWidth() == 240) {
                 //Small Screens
            i = 1;
            return i;
        } else if (display.getHeight() == 800 && display.getWidth() == 480) {
                //Large Screens
            i = 1;
            return i;
        } else if (display.getHeight() == 1280 && display.getWidth() == 800) {

               //X-large Screens
           i = 2;
            return i;
        }

    } catch (Exception e) {
        i = 0;
    }
    return i;
0
votes

If your goal is to know which layout from the layouts in the layout-small or layout-large, etc will be used, you can use this trick. You can have a view with a gone visibility and add a tag to this view indicating which kind of layout it is.

At run time, you can retrive this view and test its tag. This way you will know which layout is been used.

The android documentation gives you the information that for such and such screen dimension the corresponding type of the layout will be used. Now it is your design decision to write different layouts for different screen sizes.