How to convert DP to Pixel and Pixel to Dp?

To Convert dp to px or px to dp fisrt we need to calculate device Density DPI
we will get density DPI fromDisplaymetrics object.

Lets create DisplayMetrics object

DisplayMetrics displayMetrics= getResources().getDisplayMetrics();

Now calculate devie Density DPI 

float densityDpi=displayMetrics.densityDpi;
float defaultDPI=DisplayMetrics.DENSITY_DEFAULT;

Convert dp to px

dp*(densityDpi/defaultDPI);

Convert px to dp

px*(densityDpi/defaultDPI);

 


Flutter Questions
Android Questions