Thursday, October 19, 2017

Density Independent Pixel


In Android material design first thing that we have to think about is if the app looks as expected on all devices irrespective of the screen size. That's where density independent pixel (or DP) comes into action. Density independent pixel allows us to create assets that appears in an expected way across all devices no matter what is the resolution or density of a target device. Basically, it gives your app the consistent look that you expected.

For example, if you give a the size of a button 20px that may look great in the device that you are testing your app on, but if you run it on a different device it may not look the way you intended it to look. That's because when you set the size on pixel,it is fixed therefor in a small device if  it looks good then in a bigger device it will look really small.

So, I just said that if we give px(pixel) unit, it is fixed but what happens when we give dp (density independent pixel) unit. At runtime the system handles any scaling of the dp units. One dp is equivalent to 1px in a 160dpi(dots per inch) screen, this is the baseline  density.

The conversion equation of px and dp is:

                                                px/dp = dpi/160dpi

If we have a a screen with 213dpi screen then we'll get 1dp equal to 1.3px.

                                               px = dp*(dpi/160dpi) = dp*(213/160 ) = 1.3dp
So, this is what I know about dp. If there's any mistakes feel free to point it out but be kind :) .

Introduction To Binary Search Tree

A tree data structure is a way to hold data that looks like a tree when it's visualized. For example: All data points in a tre...