Skip to content

Instantly share code, notes, and snippets.

@XinyueZ
Created August 2, 2014 20:19
Show Gist options
  • Save XinyueZ/9a8987ddb66ac86e2ff0 to your computer and use it in GitHub Desktop.
Save XinyueZ/9a8987ddb66ac86e2ff0 to your computer and use it in GitHub Desktop.
Get Android's releaseVersion, SDK Version, Screen DPIs.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView versionTv = (TextView) findViewById(R.id.version_tv);
versionTv.setText(String.format("Version Release:%s", Build.VERSION.RELEASE));
TextView sdkTv = (TextView) findViewById(R.id.sdk_tv);
sdkTv.setText(String.format("SDK:%d", android.os.Build.VERSION.SDK_INT));
TextView resolutionTv = (TextView) findViewById(R.id.resolution_tv);
resolutionTv.setText(String.format("Resolution(DPI): %s", getDeviceResolution()));
}
/**
* Get resolution of screen which kind of dpi will be detected.
*
* @return DPI type in string.
*/
private String getDeviceResolution() {
int density = getResources().getDisplayMetrics().densityDpi;
switch (density) {
case DisplayMetrics.DENSITY_MEDIUM:
return "MDPI";
case DisplayMetrics.DENSITY_HIGH:
return "HDPI";
case DisplayMetrics.DENSITY_LOW:
return "LDPI";
case DisplayMetrics.DENSITY_XHIGH:
return "XHDPI";
case DisplayMetrics.DENSITY_TV:
return "TV";
case DisplayMetrics.DENSITY_XXHIGH:
return "XXHDPI";
case DisplayMetrics.DENSITY_XXXHIGH:
return "XXXHDPI";
default:
return "Unknown";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment