Converting pixels to dp


Question

I have created my application with the height and width given in pixels for a Pantech device whose resolution is 480x800.

I need to convert height and width for a G1 device. I thought converting it into dp will solve the problem and provide same solution for both devices.

Is there any easy way to convert pixels to dp? Any suggestions?

1
770
12/22/2015 5:10:57 PM

// Converts 14 dip into its equivalent px
float dip = 14f;
Resources r = getResources();
float px = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    dip,
    r.getDisplayMetrics()
);
983
8/30/2018 1:35:26 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon