0

My device is Nexus 7(Flo) .I want to get the real screen resolution,which the height is 1920,But the code

DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
int   mPageHeight = displayMetrics.heightPixels;

returns 1824, which is the result of 1920 minus the NavigationBar's height(48dp). I know in Immersive Mode the decorView's height is 1920.But I want to acquire the value in "onCreate" method in Activity. DecorView's height is assigned too late.

So how to get the real screen resolution height?

Or is there a reliable way to know if the NavigationBar at bottom is showing ?You know most devices don't have that NavigationBar.

WZY
  • 211
  • 1
  • 3
  • 10

2 Answers2

0

You can get the full screen resolution by using this code

View decorView = context.getActivity().getWindow().getDecorView();
Point outSize = new Point();

decorView.getDisplay().getRealSize(outSize);
Log.i("ImmersiveHeight : "+outSize.y);

set the immersie mode to your activity by using this code

final int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN 
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

decorView.setSystemUiVisibility(uiOptions);
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • I tried "activity.getWindow().getDecorView()".Unfortunatly it return 0 in "onCreate" or "onResume" method in Activity.It's assigined too late. My code which is initized in "onCreate" need that dimen.. – WZY Jan 06 '15 at 11:40
  • in "onCreate" method ,getRealSize or getRealMetrics result NullPointerException .. – WZY Jan 06 '15 at 11:53
0

This code takes effect! I get 1920x1200 finally!

 public void printHeight(){
    int width = 0, height = 0;
    final DisplayMetrics metrics = new DisplayMetrics();
    Display display = getWindowManager().getDefaultDisplay();
    Method mGetRawH = null, mGetRawW = null;

    try {
        // For JellyBean 4.2 (API 17) and onward
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealMetrics(metrics);

            width = metrics.widthPixels;
            height = metrics.heightPixels;
            Log.i("ZYStudio", "with and height:"+width+"|"+height);
        } else {
            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");
            Log.i("ZYStudio", "rawW and rawH:"+mGetRawW+"|"+mGetRawH);

            try {
                width = (Integer) mGetRawW.invoke(display);
                height = (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();
            }
        }
    } catch (NoSuchMethodException e3) {
        e3.printStackTrace();
    }
}
WZY
  • 211
  • 1
  • 3
  • 10
  • Come from http://stackoverflow.com/questions/10991194/android-displaymetrics-returns-incorrect-screen-size-in-pixels-on-ics thanks – WZY Jan 07 '15 at 02:45