title: Get the screen height in Android
body: <p>How can I get the available height of the screen in Android? I need to the height minus the status bar / menu bar or any other decorations that might be on screen and I need it to work for all devices. Also, I need to know this in the onCreate function. I know this question has been asked before but I have already tried their solutions and none of them work. Here are some of the things I have tried:</p>
  
  <p>I have tested this code on API 7 - 17. Unfortunately, on API 13 there is extra space at bottom both horizontally and vertically and on API 10, 8, and 7 there is not enough space at the bottom both horizontally and vertically. (I have not tested on obsolete APIs):</p>
  
  <pre><code>Display display = getWindowManager().getDefaultDisplay();
  DisplayMetrics metrics = new DisplayMetrics();
  display.getMetrics(metrics);
  screenWidth = metrics.widthPixels;
  screenHeight = metrics.heightPixels;
  
  TypedValue tv = new TypedValue();
  if(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB)
  {
      if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
          screenHeight -= TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
  }
  
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId &gt; 0)
      screenHeight -= getResources().getDimensionPixelSize(resourceId);
  </code></pre>
  
  <p>This does not take into account the status bar / menu bar:</p>
  
  <pre><code>Display display = getWindowManager().getDefaultDisplay();
  screenWidth = display.getWidth();
  screenHeight = display.getHeight();
  </code></pre>
  
  <p>Neither does this:</p>
  
  <pre><code>Point size = new Point();
  getWindowManager().getDefaultDisplay().getSize(size);
  screenWidth = size.x;
  screenHeight = size.y;
  </code></pre>
  
  <p>Nor this:</p>
  
  <pre><code>Point size = new Point();
  getWindowManager().getDefaultDisplay().getRealSize(size);
  screenWidth = size.x;
  screenHeight = size.y;
  </code></pre>
  
  <p>This does not work:</p>
  
  <pre><code>Display display = getWindowManager().getDefaultDisplay();
  DisplayMetrics metrics = new DisplayMetrics();
  display.getMetrics(metrics);
  // since SDK_INT = 1;
  screenWidth = metrics.widthPixels;
  screenHeight = metrics.heightPixels;
  try
  {
      // used when 17 &gt; SDK_INT &gt;= 14; includes window decorations (statusbar bar/menu bar)
      screenWidth = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
      screenHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
  }
  catch (Exception ignored)
  {
      // Do nothing
  }
  try
  {
      // used when SDK_INT &gt;= 17; includes window decorations (statusbar bar/menu bar)
      Point realSize = new Point();
      Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
      screenWidth = realSize.x;
      screenHeight = realSize.y;
  }
  catch (Exception ignored)
  {
      // Do nothing
  }
  </code></pre>
  
  <p>I then used the following code to subtract the height of the status bar and menu bar from the screen height:</p>
  
  <pre><code>int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId &gt; 0)
      result = getResources().getDimensionPixelSize(resourceId);
  screenHeight -= result;
  result = 0;
  if (screenHeight &gt;= screenWidth)
      resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
  else
      resourceId = getResources().getIdentifier("navigation_bar_height_landscape", "dimen", "android");
  if (resourceId &gt; 0)
      result = getResources().getDimensionPixelSize(resourceId);
  screenHeight -= result;
  </code></pre>
  
  <p>On API 17 it correctly calculates the height of the status bar and menu bar in portrait but not in landscape. On API 10, it returns 0. I need it to work ideally on all devices or minimum API 7. </p>