-
-
Save devrath/ed842509552bd89d1e1b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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