Last active
January 7, 2018 20:17
-
-
Save ec84b4/4a2e25fba3cdf657ff08 to your computer and use it in GitHub Desktop.
android screen size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
stackOverFlow question : http://stackoverflow.com/q/26674378/2127203