0

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.

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99

3 Answers3

0
  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;
Kalai Selvan.G
  • 482
  • 3
  • 10
  • 22
  • Yeah... I need to know what are the appropriate XML layouts are they going to access in the each **if** clauses as you described. I mean if height = 48 and width = 320, then the program going to access **layout-large** or what like wise. – AnujAroshA Jun 12 '12 at 07:26
  • Thank you for the answer. But there are two issues I can see here. First thing is when you run with a same device the getWidth() and getHeight() values is changing if there is a rotation happens. That's why I was looking for a range rather than a direct value. The next issue is when I check with several created emulators, I got 752 as getHeight() value and 1280 for getWidth() value. Then again same problem occurs. Which is, need a range rather than a definite value. If you can visit this [link](http://goo.gl/OQvyB) they are describing 5 different XML layouts. – AnujAroshA Jun 12 '12 at 08:29
  • yeah anuja..for landscape u need to create layout-land,layout-small-land,layout-large-land,layout-xlarge-land for different screens...but i dont have an idea abt 752 and 1280,researchin on that,if u get that one pls try to post it – Kalai Selvan.G Jun 12 '12 at 08:42
0

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);
MckyONeil
  • 94
  • 3
  • Why should I calculate diagonal length ? Can you give me a relationship for diagonal length and the XML layouts as I have described in my question? – AnujAroshA Jun 12 '12 at 07:22
0

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.

Prakash Nadar
  • 2,694
  • 1
  • 19
  • 20