Joe Asks What Are Dips and

Historically, programmers always designed computer interfaces in terms of pixels. For example, you might make a field 300 pixels wide, allow 5 pixels of spacing between columns, and define icons 16-by-16 pixels in size. The problem is that if you run that program on new displays with more and more dots per inch (dpi), the user interface appears smaller and smaller. At some point, it becomes too hard to read.

Resolution-independent measurements help solve this problem. Android supports all the following units:

• mm (millimeters): Size as measured by a ruler.

• dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.

• dip: Synonym for dp, used more often in Google examples.

• sp (scale-independent pixels): Similar to dp but also scaled by the user's font size preference.

To make your interface scalable to any current and future type of display, I recommend you always use the sp unit for text sizes and the dip unit for everything else. You should also consider using vector graphics instead of bitmaps (see Chapter 4, Exploring 2D Graphics, on page 73).


Android Sudoku

Figure 3.5: In landscape mode, we can't see the Exit button.

In this version, we introduce a new syntax, @+id/resid. Instead of referring to a resource ID defined somewhere else, this is how you create a new resource ID to which others can refer. For example, @+id/about_ button defines the ID for the About button, which we'll use later to make something happen when the user presses that button.

The result is shown in Figure 3.4, on page 53. This new screen looks good in portrait mode (when the screen is taller than it is wide), but how about landscape mode (wide-screen)? The user can switch modes at any time, for example, by flipping out the keyboard or turning the phone on its side, so you need to handle that.

0 0

Post a comment