Skip to content

Instantly share code, notes, and snippets.

@evandrix
Created October 17, 2011 22:11
Show Gist options
  • Save evandrix/1293997 to your computer and use it in GitHub Desktop.
Save evandrix/1293997 to your computer and use it in GitHub Desktop.
Android: Dynamic screen sizes
/**
* Sample class from Pulse
*
* Class to store and provide useful dimensions
*/
public class DimensionCalculator {
private static DimensionCalculator mInstance = null;
private int mScreenWidth;
private int mTileWidth;
/**
* This class is a singleton
*/
public static DimensionCalculator getInstance(Context context) {
if (mInstance == null) {
mInstance = new DimensionCalculator(context);
}
return mInstance;
}
/**
* Initialize the class with the proper tile size
*/
protected DimensionCalculator(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
mScreenWidth = Math.min(context.getResources().getDisplayMetrics().widthPixels,
context.getResources().getDisplayMetrics().heightPixels);
int numTiles = 3;
int tileGap = 2;
mTileWidth = (int) ((mScreenWidth - 4 * tileGap) / numTiles);
}
/**
* Return the appropriate tile size for this device
*/
public int getTileWidth() {
return mTileWidth;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment