Skip to content

Instantly share code, notes, and snippets.

@akbarsha03
Created July 15, 2014 07:03
Show Gist options
  • Save akbarsha03/1c8a677b401bad693038 to your computer and use it in GitHub Desktop.
Save akbarsha03/1c8a677b401bad693038 to your computer and use it in GitHub Desktop.
Get required view size dynamically in percentage depends upon the Screen size
/**
* Get the area of the screen size. following calculation done with the help
* of Ashik :D
*
* @param context
* @param percentage
* @return intRequiredCellSize
* @author Ashik Ali
*/
public static int getCellSizeForScreen(Context context, double percentage) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
/**
* Calculate the percentage of the current_screen_area. 0.01 represents
* the percentage of the cell size in the total percentage of the screen
* size
*/
int required_squre_area = (int) ((height * width) * (percentage / 100));
int required_squre_size = (int) Math.sqrt(required_squre_area);
return required_squre_size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment