Convert DP Unit to Equivalent Pixels in Android

Community Member
Posted on 2 weeks agoThe following code snippet shows how to converts DP unit to equivalent pixels, depending on device density. It returns a float value to represent px equivalent to dp depending on device density.
public static float convertDpToPixel(float dp, Context context){ Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * (metrics.densityDpi / 160f); return px; }
Related posts
- Convert Speech to Text in Android Application
- How to Programmatically Get Application Version in Android?
- How to Use Custom Chrome Tab in Android
- How to Add Rate On Google Play Button in Android
- Invite People to Download App Using Firebase AppInviteInvitation
- How to Disable Past Dates in Custom Calendar View Android