Wakka02
Wakka02

Reputation: 1541

Detect screen size in Android

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

Upvotes: 0

Views: 6222

Answers (4)

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

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

Upvotes: 1

Karan Mavadhiya
Karan Mavadhiya

Reputation: 1052

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

Upvotes: 4

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9735

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();

Upvotes: 1

Brijesh Thakur
Brijesh Thakur

Reputation: 6788

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;    
}

Upvotes: 0

Related Questions