Created
February 27, 2018 07:36
-
-
Save kevinhaitsma/6d4ef868b44d860fa5a44bc731217c0f to your computer and use it in GitHub Desktop.
Convert dp to px and px to dp in Kotlin for Android
This file contains 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
import android.content.Context | |
import android.content.res.Resources | |
import android.util.DisplayMetrics | |
/** | |
* Provides utilities for metrics. | |
* | |
* Original at: | |
* @see <a href="https://stackoverflow.com/a/9563438/8877070">stack overflow answer</a> | |
* | |
* @author kevin | |
*/ | |
object MetricsUtil { | |
/** | |
* This method converts dp unit to equivalent pixels, depending on device density. | |
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels | |
* @param context Context to get resources and device specific display metrics. If you don't have | |
* access to Context, just pass null. | |
* @return A float value to represent px equivalent to dp depending on device density | |
*/ | |
fun convertDpToPixel(dp: Float, context: Context?): Float { | |
return if (context != null) { | |
val resources = context.resources | |
val metrics = resources.displayMetrics | |
dp * (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT) | |
} else { | |
val metrics = Resources.getSystem().displayMetrics | |
dp * (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT) | |
} | |
} | |
/** | |
* 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. If you don't have | |
* access to Context, just pass null. | |
* @return A float value to represent dp equivalent to px value | |
*/ | |
fun convertPixelsToDp(px: Float, context: Context?): Float { | |
return if (context != null) { | |
val resources = context.resources | |
val metrics = resources.displayMetrics | |
px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT) | |
} else { | |
val metrics = Resources.getSystem().displayMetrics | |
px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment