Skip to content

Instantly share code, notes, and snippets.

@devrath
Forked from Tarubali/ScreenUtility.java
Last active August 29, 2015 14:24
Show Gist options
  • Save devrath/ed842509552bd89d1e1b to your computer and use it in GitHub Desktop.
Save devrath/ed842509552bd89d1e1b to your computer and use it in GitHub Desktop.
public class ScreenUtility {
private Activity activity;
private float dpWidth;
private float dpHeight;
public ScreenUtility(Activity activity) {
this.activity = activity;
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = activity.getResources().getDisplayMetrics.density;
dpHeight = outMetrics.heighPixels / density;
dpWidth = outMetrics.widthPixels / density;
}
public float getWidth() { return dpWidth; }
public float getHeight() { return dpHeight; }
}
//USAGE
//Declare an instance of the ScreenUtility class in your activity onCreate
//like
//ScreenUtility utility = new ScreenUtility(this);
//then
//utility.getWidth() and utility.getHeight() will return the appropriate values
//output
//using Log to console or Toast to screen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment