Ch 4: Externalizing Resources Flashcards
Externalizing Resources:
Why is it a good practice?
- Easier to maintain, update and manage
- Allows for creation of alternative resource values:
- Supports internationalization
- Support for variations in hardware, particularly screen size and resolution
- Support for increasing Accessibility
Externalizing Resources:
Overview of how Android handles external resources
- In a project, resources are stored a special resources folder, ‘res’ which includes subfolders for various kinds of resources, with parallels for different configurations
- Android dynamically selects resources with the “correct” configuration automatically
- A special class file ‘R’ is created, containing references to each of the resources i
- Allows referencing the resources within code
- Most built in Android components are built to work with these references
- Direct values of resources can also be obtained by accessing the application’s “Resources” class
- This contains getters and setters for each available resource type
‘res’ folder
Overview
- Within the project hierarchy, application resources are stored in the ‘res’ folder
- Each of the available resources is stored in subfolders, grouped by resource type
- Different sets of resources are supported by using a parallel directory structure:
- Alternate versions of the subfolders have the same name, suffixed with a hyphen and a qualifier
- By default, the res folder contains subfolders:
- ‘values’
- ‘mipmap’
- ‘layout’
- But there are several others
Resource filename requirements
Resource filenames should only contain:
- Lowercase letters
- numbers
- the period ( . ) or underscore ( _ ) symbols
- nothing else
List of Resource Types
and associated Subfolders in ‘res’
-
Simple Values
- ‘values’
-
Drawables
- ‘drawable’
-
MipMaps(Icons)
- ‘mipmap’
-
Layouts
- ‘layout’
-
Animations
- Property Animations - ‘animator’
- View Animations - ‘anim’
- Frame Animations - ‘drawable’
Resource Types:
Simple Values
- Includes:
- strings
- colors
- dimensions
- styles
- boolean or integer values
- string or typed arrays
- All are stored within separate XML files in the ‘res/values’ folder
- By default:
- strings.xml
- colors.xml
- styles.cml
Resource Types:
Simple Values:
Strings
Should be specified in res/values/strings.xml
- Specified with the <string> tag:
</string><ul>
<li><string>Hello!</string></li>
</ul></string> - Special characters must be escaped with a backslash
- Simple text styling is supported by using HTML tags (,,)
- Can also use resource strings as input for formatting strings at runtime
- Can define alternate plural forms
Resource Types:
Simple Values:
Strings:
Using Formatted Strings
- Write the resource string with the usual formatting insertions: “%s, %d” etc
- Then, use the string as input to the String.format() method ( or a method that uses it)
- Cannot include the usual HTML text styling tags, however
- Instead, use the formatted version:
- My bold text (inserted string) becomes
- <b>My bold text</b>: %s
- Then, at runtime, after formatting, use the Html.fromHtml() method to apply the styling
- Instead, use the formatted version:
Resource Types:
Simple Values:
Strings:
Alternative Plural Forms
Allows you to specify an alternative form for any of: ‘zero’, ‘one’, ‘multiple’, ‘few’, ‘many’ or ‘other’ quantities.
- Define a <plurals></plurals> block, with one item for each quanity to be supported:
<plurals></plurals>
<item>One Goose</item>
<item>%d Geese</item>
- At runtime, use the getQuantityString() method on your app’s Resources object:
String geeseStr = resources.getQuantityString(R.plurals.goose_count, gCount, gCount);
- Quantity string is passed in twice:
- Once to return correct plural string,
- Once as an input parameter to fill in for “%d”
Resource Types:
Values:
Colors
Defined in res/values/colors.xml
- Use the <color> tag to define a new color resource</color>
- Specify the value using a # symbol, followed by the desired Alpha-Red-Green-Blue values in hexadecimal
- Alpha is optional
- Can use either 1 or 2 digit representation
- Example:
- <color>#A4C639</color>
- <color>#770000FF</color>
Resource Types:
Values:
Dimensions
Dimensions are commonly referenced within style and layout resources.
Useful for defining layout values such as borders and heights.
- specify with the <dimen> tag, followed by a value and a scale identifier</dimen>
- It is best practice to use either density independent(dp) or scalable pixels(sp)
Resource Types:
Values:
Dimensions:
Scales
Scales:
- dp - density-independent pixels
- sp - scalable pixels
- px - screen pixels
- in - physical inches
- pt - phyiscal points
- mm - physical millimeters
Resource Types:
Values:
Dimensions:
Best Practices
It is best practice to use either Density Independent or Scalable pixels.
These let you define a dimension using relative scales that account for different screen resolutions and densities, simplifying scaling on different hardware.
Scalable pixels are particularly good for defining font sizes. They automatically scale if the user changes the system font size.
Resource Types:
Values:
Styles
Styles let an app maintain a consistent look and feel.
Specifies the attribute values used by Views:
Most commonly colors, borders and font sizes.
- Defined in res/values/styles.xml
- Specify with a <style></style> tag, including name attribute
- Contains <item></item> tags that define various attributes
- Styles can inherit from others, allowing them to only make minor modifications to existing styles
- Use the “parent” attribute on the <style></style> tag
- Example:
<style></style>
<item>14sp</item>
<item>#111</item>
Resource Types:
Drawables
Stored as individual files in the res/drawable folder
Basic Files:
- Simple Image: bitmaps(preferred as PNG)
- NinePatches (stretchable PNG images)
- scalable Vector Drawables
Complex Composite Files:
- (Typically define frame animations)
- LevelListDrawables
- StateListDrawables
The preferred format for Drawable bitmap resources is PNG, but JPG and GIF are also supported