Skip to content
Search

Android:Dimension

lastmod 2016-05-27created 2016-05-271 edits

A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp.

In Android development, building a UI that fits the resolutions of various phones is quite difficult. Ultimately, you should define resources in xml as much as possible and split them into folders by resolution. That is the case when loading and using resources dynamically. Among these, when you take a resource from the dimens.xml file for Text Size and call setTextSize(), the text comes out much larger than intended.

value-xhdpi/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--  Record table row size -->
    <dimen name="record_row_height">24dp</dimen>
    <dimen name="record_default_text">15dip</dimen>
</resources>

As above, you can define high-resolution resources in the value-xhdpi folder. Of course, the same dimens.xml must also be defined in the value-hdpi folder. To use the resource in source code, do the following.

TextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, res.getDimension(R.dimen.record_default_text));

Like this, you must state explicitly that the resource being set is in pixels. The problem is that getDimension() returns the value in pixels. Because we specified dip in dimens.xml, getDimension() converts it to pixels on its own before returning it. However, the default unit of setTextSize() is sp, not pixel, which is what causes the problem.

When applying it in a Layout XML, apply it to an attribute like the one below.

android:textSize="@dimen/record_row_height"

How to get the value set in dimens.xml as DIP

Create a dimens.xml file under the res/values folder of the Android project, and write the following.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- size -->
    <dimen name="size">142px</dimen>
</resources>

In the Java code that reads this value, load the pixel value as a DIP value appropriate for the current device as follows.

// returns the number of pixels for 123.4dip
int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 123.4, getResources().getDisplayMetrics());

The following units of measure are supported by Android

Density-independent Pixels (dp, dip)
An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
Scale-independent Pixels (sp)
This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
Points (pt)
1/72 of an inch based on the physical size of the screen.
Pixels (px)
Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
Millimeters (mm)
Based on the physical size of the screen.
Inches (in)
Based on the physical size of the screen.

Favorite site