Skip to content

Instantly share code, notes, and snippets.

@ec84b4
Last active January 7, 2018 20:17
Show Gist options
  • Save ec84b4/4a2e25fba3cdf657ff08 to your computer and use it in GitHub Desktop.
Save ec84b4/4a2e25fba3cdf657ff08 to your computer and use it in GitHub Desktop.
android screen size
public void setScreenSize(Context context) {
int x, y, orientation = context.getResources().getConfiguration().orientation;
WindowManager wm = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE));
Display display = wm.getDefaultDisplay();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point screenSize = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealSize(screenSize);
x = screenSize.x;
y = screenSize.y;
} else {
display.getSize(screenSize);
x = screenSize.x;
y = screenSize.y;
}
} else {
x = display.getWidth();
y = display.getHeight();
}
int width = getWidth(x, y, orientation);
int height = getHeight(x, y, orientation);
}
private int getWidth(int x, int y, int orientation) {
return orientation == Configuration.ORIENTATION_PORTRAIT ? x : y;
}
private int getHeight(int x, int y, int orientation) {
return orientation == Configuration.ORIENTATION_PORTRAIT ? y : x;
}
@ec84b4
Copy link
Author

ec84b4 commented Dec 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment