An introduction to resource management in Android. Namely, the presentation discusses how to correctly account for different screen densities and sizes.
2. Motivation
¤ It’s always good practice to keep non-code resources,
e.g., images and string constants, external to your code
2
3. Upsides
¤ By externalizing resources, you make them easier to
maintain, update and manage
¤ Example: support a new screen size
¤ Since resources are externalized, it is possible to alter the
code without modifying the resources (and viceversa)
¤ Android automatically selects the correct resources
without you having to write code
¤ Example: select the proper language
3
4. Creating resources
¤ Application resources are stored
under the res/ folder:
¤ Grouped by resource type
¤ Stored in subfolders (one for
each resource type)
¤ When your application is built, these
resources will be compiled and
compressed as efficiently as possible
and included in your application
package
4
5. Creating resources
¤ Android supports the externalization of:
¤ Simple values, such as strings and colors
¤ Complex values
¤ Images (drawables)
¤ Animations
¤ UI elements
¤ Themes
¤ Menus
¤ Layouts
5
6. Creating resources
¤ Android supports the externalization of:
¤ Simple values, such as strings and colors
¤ Complex values
¤ Images (drawables)
¤ Animations
¤ UI elements
¤ Themes
¤ Menus
¤ Layouts
6
7. Dealing with different screen sizes
¤ Units of measurements:
¤ You must avoid using absolute pixels to define distances or sizes,
as different screens have different pixel densities
7
dp Density-independent pixel. 1 dp is equivalent to one
pixel on a 160 dpi screen
sp Scale-independent pixel. An sp is the same base unit
as dp, but it is (possibly) scaled by the user’s preferred
text size. It should be used when defining text size
pt Point. A point is defined to be 1/72 of an inch
px Pixel. Corresponds to actual pixels on the screen
9. Dealing with different screen sizes
¤ Android defines and recognizes four screen densities
¤ Medium density: mdpi, 160 dpi
¤ High density: hdpi, 240 dpi
¤ Extra high density: xhdpi, 320 dpi
¤ Extra extra high density: xxhdpi, 480 dpi
¤ In the Android terminology, screen densities are called
density buckets
9
10. Provide alternative bitmaps
¤ You should always provide bitmap resources tailored to
each of the available density bucket
¤ To do so, each single image should be provided at the
following scales:
¤ Normal density (mdpi): 1.0x
¤ High density (hdpi): 1.5x
¤ Extra-high density (xhdpi): 2.0x
¤ Extra extra high density (xxhdpi): 3.0x
10
11. Example
¤ Example: the same image must be generated at
¤ 100x100 for mdpi
¤ 150x150 for hdpi,
¤ 200x200 for xhdpi
¤ 300x300 for xxhdpi
11
13. Drawables
¤ Each density bucket has an associated subfolder in res/
¤ Namely, such subfolders are:
¤ res/drawable-mdpi
¤ res/drawable-hdpi
¤ res/drawable-xhdpi
¤ res/drawable-xxhdpi
¤ The preferred format for a bitmap resource is PNG, but
JPG and GIF images are also supported
13
14. Scaling the same image
14
res/drawable-mdpi
res/drawable-hdpi
res/drawable-xhdpi
res/drawable-xxhdpi
15. Simple values:
string resources
¤ Strings, String arrays and plurals are stored in
/res/values/strings.xml
15
<resources>
<string name="app_name">TakeNotes</string>
<string name="action_settings">Settings</string>
</resources>
[...] android:label="@string/app_name" [...]
¤ If you remember, they were used in AndroidManifest.xml:
16. Simple values:
string resources
¤ String resources are specified with the string tag
¤ Font formatting: it is possible to format strings by using
HTML tags (e.g., <b> or <i>)
¤ A string array defines an array of strings
16
<string name="stop_message">Stop</string>
<string-array name="string-array”>
<item>Item1</item>
<item>Item2</item>
</string-array>
17. Simple values:
color resources
¤ Color profiles are stored in /res/values/colors.xml
¤ Use the color tag to define a new color resource
¤ A color is specified using a # symbol followed by the
alpha channel
¤ Examples:
¤ #RRGGBB (RGB values)
¤ #AARRGGBB (RGB values + alpha channel)
17
<color name="opaque-blue">#CD43A2</color>