In activity_main.xml, you specified attribute values in terms of dp
units.
Now it is time to learn what they are.
Sometimes you need to specify values for view attributes in terms of specific sizes (usually in pixels, but sometimes points, millimeters, or inches). You see this most commonly with attributes for text size, margins, and padding. Text size is the pixel height of the text on the device’s screen. Margins specify the distances between views, and padding specifies the distance between a view’s outside edges and its content.
As you saw in the section called Adding an Icon, Android automatically scales images to different screen pixel densities using density-qualified drawable folders (such as drawable-xhdpi). But what happens when your images scale, but your margins do not? Or when the user configures a larger-than-default text size?
To solve these problems, Android provides density-independent dimension units that you can use to get the same size on different screen densities. Android translates these units into pixels at runtime, so there is no tricky math for you to do (Figure 2.9).
In practice and in this book, you will use dp and sp almost exclusively. Android will translate these values into pixels at runtime.
3.141.24.134