0

I need to disable a certain button in my app if the screen size of the Android device is considered SMALL (320dp x 426dp units). I am currently using:

if (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
    //Is small size
} else {
    //Is not small size
}

to determine the screen size of the device, however this never seems to enter the condition of Configuration.SCREENLAYOUT_SIZE_SMALL. I did printouts and found that the value actually equates to Configuration.SCREENLAYOUT_SIZE_NORMAL!

The answer from Alex Lockwood here: How do I get the ScreenSize programmatically in android is my reference for this solution, but as J R P noted on his answer, everything seemed to equate to Configuration.SCREENLAYOUT_SIZE_NORMAL just like mine.

How do I reliably define the screen size of the device? I am currently testing on a Samsung Galaxy S3, and by all means this should have been a "small" device.

Thanks in advance, Rei

Community
  • 1
  • 1
Wakka02
  • 1,491
  • 4
  • 28
  • 44
  • Instead of doing above, why don't you create different layouts for ldpi and mdpi? In ldpi you won't include the button and in code you will check if that button is present in layout or not. – gunar Jul 10 '13 at 10:09
  • there is a ( missing in your code. – njzk2 Jul 10 '13 at 10:12
  • Because it's not screen pixel density I'm looking at, more of screen size... – Wakka02 Jul 11 '13 at 02:39

4 Answers4

4

Try This TO Display Dimensions In Pixels

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

For More Use This StackQuestion

Community
  • 1
  • 1
Karan Mavadhiya
  • 1,042
  • 8
  • 23
1

here comes two functions to get screen size W and H

    public static int getScreenSizeW(Context context) {
            int screenSizeW = 320;//default

            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            try {
                screenSizeW = DisplayMetrics.class.getField("widthPixels").getInt(displayMetrics);
            } catch (Exception e) {
            }

            return screenSizeW;
        }

public static int getScreenSizeH(Context context) {
            int screenSizeH = 480;//default

            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            try {
                screenSizeH = DisplayMetrics.class.getField("heightPixels").getInt(displayMetrics);
            } catch (Exception e) {
            }

            return screenSizeH;
        }

and here comes another way to get the display size:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
size.x = display.getWidth();
size.y = display.getHeight();
Yilmaz Guleryuz
  • 9,313
  • 3
  • 32
  • 43
1

Try this code..

int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;

String toastMsg;
switch(screenSize) {
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        toastMsg = "Large screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        toastMsg = "Normal screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        toastMsg = "Small screen";
        break;
    default:
        toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();

Also have a look at

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

Use the below code.

public boolean isPhone(Context activityContext) {

 if(null!=activityContext)
 {
  boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout &   Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
  boolean large = ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);   
  return (xlarge || large)?false:true;
 }
return false;    
}
Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29