Skip to content

Instantly share code, notes, and snippets.

@huuphuoc1396
Created September 17, 2020 02:41
Show Gist options
  • Select an option

  • Save huuphuoc1396/0053ed88e02eee6b7308d1661711500e to your computer and use it in GitHub Desktop.

Select an option

Save huuphuoc1396/0053ed88e02eee6b7308d1661711500e to your computer and use it in GitHub Desktop.
Convert dp, px, sp among each other (ref: https://bit.ly/35Qqyx4)
public static int dpToPx(float dp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
public static int dpToSp(float dp, Context context) {
return (int) (dpToPx(dp, context) / context.getResources().getDisplayMetrics().scaledDensity);
}
public static int spToPx(float sp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment