In my Android application I have pixels(69px) and I need to convert this pixels into dip(Density Independent Pixels).
Any suggestions?
Pixel values can be converted to DP by dividing the pixel value by the screen pixel density.
One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density). Android translates this value to the appropriate number of real pixels for each other density.
SP: is an abbreviation for Scale independent pixels. It is the same as the dp unit, but it is additionally scaled according to the user's font size selection. DP: A virtual pixel unit used to communicate layout dimensions or location in a density-independent manner while creating UI layout.
Just divide your value in pixels by DisplayMetrics.density.
Hope this will be helpful
Resources r = getResources();
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 69, r.getDisplayMetrics());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With