Skip to content

Instantly share code, notes, and snippets.

View rishadbaniya's full-sized avatar
:shipit:
Busy hacking myself

Rishad Baniya rishadbaniya

:shipit:
Busy hacking myself
View GitHub Profile
@rishadbaniya
rishadbaniya / DpToPxAndPxToDp
Created January 4, 2021 03:07 — forked from laaptu/DpToPxAndPxToDp
Android convert dp to px and vice versa
public static float convertPixelsToDp(float px){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return Math.round(dp);
}
public static float convertDpToPixel(float dp){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
@rishadbaniya
rishadbaniya / DpToPxAndPxToDp
Created January 4, 2021 03:07 — forked from laaptu/DpToPxAndPxToDp
Android convert dp to px and vice versa
public static float convertPixelsToDp(float px){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return Math.round(dp);
}
public static float convertDpToPixel(float dp){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);