Intro to Android

Dimensions & Colors

My approach to dimensions and styles has evolved over time as I get exposed to new teams and new designers. I'll show you what it took me a while to discover for myself in hopes of saving you some headaches down the road.

When I started with Android, the team and I gleefully threw hard-coded values all over the place, often duplicating values in the same file! Not only did this litter the code, but it made debugging view issues more difficult.

Throughout this chapter I'll show you a scalable way to manage these resources.


It's straightforward to create a dimension. You'll need a dimens.xml file in res/values/:

    <dimen name="my_dimen">25dp</dimen>

You can reference them in styles:

<style name="YourStyle">
    <item name="android:layout_width">@dimen/my_dimen</item>

Or inline in your XML:


Or even in Java:

    float dimen = getResources().getDimension( R.dimen.my_dimen );


It might not be immediately apparent, but using dimensions with resource qualifiers (covered in depth later) allow you to create responsive components. You'll see this in action in the "Flexible Dimensions" lab.


Abstracting colors gives you a convenient place to store and name your colors instead of using hard coded hex values all over the place. Having names associated with the colors makes it easier to maintain view changes.

Plus you have an added bonus of previews of your color swatches and a color picker (including dropper!) when you are open colors.xml in IntelliJ.


You can abstract many types of values. Check out this article for more information: