Skip to content

Instantly share code, notes, and snippets.

@hassantafreshi
Last active January 1, 2016 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hassantafreshi/4bbbaf252d26a84f642e to your computer and use it in GitHub Desktop.
Save hassantafreshi/4bbbaf252d26a84f642e to your computer and use it in GitHub Desktop.
density android Class
import android.content.Context;
import android.util.Log;
/**
* Created by hassanTafreshi on 1/1/2016.
* Get density of Android screen size
*/
public class densityClass {
public float density;
private Context context;
public String screenDpi;
public void setDensity(Context context) {
this.context=context;
}
public void getDensity() {
/*
DisplayMetrics displayMetrics = this.getApplicationContext().getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
*/
this.density = context.getResources().getDisplayMetrics().density;
Log.e("log:", "density:" + density);
this.density= this.density*100;
// return dp;
}
public void getScreenSize() {
switch ((int) density) {
case 75:
screenDpi = "ldpi";
break;
case 100:
screenDpi = "mdpi";
break;
case 150:
screenDpi = "hdpi";
break;
case 200:
screenDpi = "xhdpi";
break;
case 300:
screenDpi = "xxhdpi";
break;
case 400:
screenDpi = "xxxhdpi";
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment