Skip to content

Instantly share code, notes, and snippets.

@carlosgub
Created June 21, 2019 22:15
Show Gist options
  • Save carlosgub/6a13a499d878ca9af34ccb5451c93768 to your computer and use it in GitHub Desktop.
Save carlosgub/6a13a499d878ca9af34ccb5451c93768 to your computer and use it in GitHub Desktop.
Px to Dp
/**
* This method converts device specific pixels to density independent pixels.
*
* @param px A value in px (pixels) unit. Which we need to convert into db
* @param context Context to get resources and device specific display metrics
* @return A float value to represent dp equivalent to px value
*/
public float convertPxToDp(Context context, float px) {
return px / context.getResources().getDisplayMetrics().density;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment