Working with Dimensions

To specify the size of a user interface control such as a Button or TextView control, you need to specify different kinds of dimensions. You tag dimension resources with the <dimen> tag and store them in the resource file /res/values/dimens.xml. This XML resource file is not created by default and must be created manually.

Here is an example of a dimension resource file:

<?xml version="i.0" encoding="utf-8"?> <resources>

<dimen name="thumbDim">100px</dimen> </resources>

Did you Know?

Each dimension resource value must end with a unit of measurement. Table 4.3 lists the dimension units that Android supports.

TABLE 4.3 Dimension Unit Measurements Supported in Android

Type of Measurement

Description

Unit String

Pixels

Actual screen pixels

px

Inches

Physical measurement

in

Millimeters

Physical measurement

mm

Points

Common font measurement

pt

Density-independent pixels

Pixels relative to 160dpi

dp

Scale-independent pixels

Best for scalable font display

sp

The following code retrieves a dimension resource called thumbDim using the getDimension() method:

float thumbnailDim = getResources().getDimension(R.dimen.thumbDim);

The following code retrieves a dimension resource called thumbDim using the getDimension() method:

float thumbnailDim = getResources().getDimension(R.dimen.thumbDim);

0 0

Post a comment