13

I have tried different ways to get the screen size of the device, but it always returned me the wrong size (791x480 instead of 854x480). It might be due to the navigation bar. My device is currently running JellyBean 4.1.1.

I tried:

Display display = getWindowManager().getDefaultDisplay();  
Point size = new Point();
display.getSize(size);
OpenGLRenderer.widthScreen = size.x;
OpenGLRenderer.heightScreen = size.y;

And:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String resolution = dm.widthPixels + "x" + dm.heightPixels;

I'm using the following code to make the app full screen:

setContentView(mGLSurfaceView);
mGLSurfaceView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

I noticed that in the logcat, the right size is returned:

01-01 03:05:06.967: I/InputReader(1961): 
    Device reconfigured: id=8, name='cyttsp-spi', 
    surface size is now 480x854, mode is 1

In my AndroidManifest:

uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"

I used:

Why am I not able to get the real size (480x854)? Or how can I use the surface returned by the “Device reconfigured”?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Trois
  • 133
  • 1
  • 1
  • 6
  • 1
    try getRealMetrics http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics) – Jameo Nov 29 '12 at 14:55
  • I'm confused. You're asking how to get the physical size of the device, then you say you get the size minus the navigation bar. Then you say you have the physical size of the device. Where's your problem exactly? – DeeV Nov 29 '12 at 15:43
  • @deev You’re right, I’m asking how to get the physical size of the device, I just suppose that the size returned to me is the one minus the nav bar. I’m asking for a way to write a code wich will give the physical size for any device. – Trois Nov 30 '12 at 13:07
  • @jameo I’m trying to use your method, but when I try this : 'final DisplayMetrics metrics = new DisplayMetrics(); Display display = getWindowManager().getDefaultDisplay(); display.getRealMetrics(metrics);' I got an error for getRealMetrics that says _The method getRealMetrics(DisplayMetrics) is undefined for the type Display_. Any idea ? – Trois Nov 30 '12 at 13:07
  • You said in Logcat that you got the size that you expected. Log isn't going to change the variables for you, so I'm still confused. – DeeV Nov 30 '12 at 13:11
  • The line with the good value that I see in the logcat isn't a result that I control. It is not due to a line of code that I wrote, it is return 'automatically', so I'm asking what function should I use to get the good size (or how can I get the good value that is returned automatically, to put it in a variable). If it's more clear. – Trois Nov 30 '12 at 13:20

4 Answers4

14

Try calling

    display.getRealSize();

DISCLAIMER: I did not realize this, but this will only work for API 17 (Android 4.2/JellyBean) and up.

Andrew Campbell
  • 17,665
  • 2
  • 16
  • 25
12

Try this

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

try {

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

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

        try {
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
Ahmed
  • 814
  • 2
  • 19
  • 38
0

I was facing the same problem on an Android 3.2 tablet. I tested both getRealSize and getDisplayMetrics. They are expected to crash as they meant to be available on API 17. But strangely getDisplayMetrics doesn't crash on my 3.2 and returns the correct dimension. getRealSize crashes.

Herve Thu
  • 1,207
  • 1
  • 12
  • 10
0

if you want to get the size in the activity, you can make it by the following code.

findViewById(android.R.id.content).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            View view =  findViewById(android.R.id.content);
            Log.e("test", "root view height is " + view.getHeight());

        }
    });
Liu Tom
  • 397
  • 2
  • 9