user3413759
user3413759

Reputation:

How to get phone screens height and width

I use this code to find the Height and width of the screen

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

But I get 1080 x 1920 for samsung galaxy S4 and 800 x 1280 for Nexus 7. But I actually need the the orginal heigth and width. How to get it.

Upvotes: 3

Views: 174

Answers (4)

user2554178
user2554178

Reputation: 1

Normalize the pixel values using densityDpi value. This will give dimensions in terms of DP (density independent pixels).

    int width =  (displayMetrics.widthPixels * 160)/displayMetrics.densityDpi; 
    int height = (displayMetrics.heightPixels * 160)/displayMetrics.densityDpi;

Upvotes: 0

M D
M D

Reputation: 47807

Try this way:

 final DisplayMetrics metrics = new DisplayMetrics(); 
        Display display = getWindowManager().getDefaultDisplay();     
        Method mGetRawH = null,mGetRawW = null;
        int realWidth=0,realHeight=0;

             // For JellyBeans and onward
            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){

                display.getRealMetrics(metrics);
                realWidth = metrics.widthPixels;
                realHeight = metrics.heightPixels;

            } else{
            // Below Jellybeans you can use reflection method 

            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");

            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);       
       }

    System.out.print(realWidth);
    System.out.print(realHeight);

Upvotes: 1

Renjith Krishnan
Renjith Krishnan

Reputation: 2616

I Hope this may help

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93542

If you mean the size without subtracting room for the status bars and other decorations- use Display.getRealSize().

Upvotes: 1

Related Questions