Android How to - Get Screen Size








Question

We would like to know how to get Screen Size.

Answer

/*w  w  w  . j  av  a2  s.com*/
import android.app.Activity;
import android.graphics.Point;
import android.view.Display;

class ActivityAttrUtils {
  
  /**
   * Method that get the Screen Size
   * @param activity Actual Activity
   * @return array type int 
   */
  public static int[] getScreenSize(Activity activity){
      
      //Obtain default display
      Display display = activity.getWindowManager().getDefaultDisplay();
      Point size = new Point();
      
      //Set Size in case of ICS
      if(android.os.Build.VERSION.SDK_INT >= 13){
          display.getSize(size);
          return new int[] {size.x, size.y};
      } else {
          return new int[] {display.getWidth(), display.getHeight()};
      }
  }
}