-
-
Save trangcongthanh/581f8075985ba3d548488aaa6152c5a0 to your computer and use it in GitHub Desktop.
Android convert int to dp
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
OBS: O var_number_int é a varável que recebera o valor a ser convertido em DP | |
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, var_number_int, getResources().getDisplayMetrics()) | |
##KOTLIN | |
val Int.dp: Int | |
get() = (this * Resources.getSystem().displayMetrics.density + 0.5f).toInt() | |
val Float.dp: Int | |
get() = (this * Resources.getSystem().displayMetrics.density + 0.5f).toInt() | |
Usage then would be cleaner, for example: | |
// before: | |
view.setPadding(ctx.dip(8), ctx.dip(16), ctx.dip(8), ctx.dp(16)) | |
// after: | |
view.setPadding(8.dp, 16.dp, 8.dp, 16.dp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment