Ch 4: Externalizing Resources Flashcards

1
Q

Externalizing Resources:

Why is it a good practice?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Externalizing Resources:

Overview of how Android handles external resources

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

res’ folder

Overview

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Resource filename requirements

A

Resource filenames should only contain:

  • Lowercase letters
  • numbers
  • the period ( . ) or underscore ( _ ) symbols
  • nothing else
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List of Resource Types

and associated Subfolders in ‘res’

A
  • Simple Values
    • ‘values’
  • Drawables
    • ‘drawable’
  • MipMaps(Icons)
    • ‘mipmap’
  • Layouts
    • ‘layout’
  • Animations
    • Property Animations - ‘animator’
    • View Animations - ‘anim’
    • Frame Animations - ‘drawable’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Resource Types:

Simple Values

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Resource Types:

Simple Values:

Strings

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Resource Types:

Simple Values:

Strings:

Using Formatted Strings

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Resource Types:

Simple Values:

Strings:

Alternative Plural Forms

A

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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Resource Types:

Values:

Colors

A

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>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Resource Types:

Values:

Dimensions

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Resource Types:

Values:

Dimensions:

Scales

A

Scales:

  • dp - density-independent pixels
  • sp - scalable pixels
  • px - screen pixels
  • in - physical inches
  • pt - phyiscal points
  • mm - physical millimeters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Resource Types:

Values:

Dimensions:

Best Practices

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Resource Types:

Values:

Styles

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Resource Types:

Drawables

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Resource Types:

MipMaps

A

res/mipmap

  • Contains image resources that are stored at high resolution and not removed for optimization, as some Drawables are for unused screen resolutions
  • It is considered best practice to store the application’s launcher icon image here
17
Q

Resource Types:

Layouts

A

Layout resources enable you to decouple the Presentation Layer from Business Logic.

UI Layouts are designed in XML.

Stored in res/layout

  • Define UI for any visual component, including Activities, Fragments and Widgets
  • layout resources are “inflated” into the UI at runtime using either setContentView() or an Inflator object
  • Each layout is stored in its own XML file that should correspond to the Activity or Fragment name.
18
Q

Animations:

3 Kinds

A
  • Property Animations
    • Animate property changes
  • View Animations
    • Transition animations on views
  • Frame Animations
    • Classic “frame-by-frame” animations
19
Q

Animations:

Property Animations

Overview

A

A “tweened”(interpolated) animation that applies incremental changes when changing some property on an object.

  • Defined in res/animator folder
  • Each Property Animation is stored in a separate XML file
  • Can be defined for a specific property or be a generic animator that can be allocated to any property and object
  • Permanently changes the property targeted
  • Defines an Object Animator in XML using the <objectanimator></objectanimator> tag
  • Object Animators can be grouped to create complex animations using the <set> tag</set>
20
Q

Animations:

View Animations

Overview

A

“Tweened” animations that can be applied to rotate, move, fade and stretch a View

  • Stored in individual XML files in res/anim
  • Each animation is specified using the tags:
    • <alpha></alpha>
    • <scale></scale>
    • <rotate></rotate>
    • <translate></translate>
  • Along with some attributes defining initial and start values, duration, etc
  • animations can be grouped using the <set> tag</set>
  • NOTE: It is better practice to avoid View Animations and use Property Animations instead, whenever possible
21
Q

Animations:

Frame-by-Frame Animations

Overview

A

Represent a sequence of Drawables, each displayed for a specified duration.

  • Stored in the res/drawable folder as individual XML files, along with image files that make up their frames
  • Defined as an Animation List: <animation-list></animation-list>:
    • Includes an <item></item> tag that represents each frame, referencing the image file and duration
  • In many cases, multiple resolutions of each of the Drawables must be included
  • Animation is accessed at runtime by:
    • assigning the animation file as an image object
    • Casting it as an AnimationDrawable object
    • Using the AnimationDrawable methods to start, stop, pause, etc
22
Q

Using Resources:

Basic Topics

A
  • System Resources
  • Accessing Resources in code
  • Referencing Resources within other Resources
  • Defining Alternative Resources
23
Q

Using Resources in Code:

The R class

A

R

  • static, generated class created when project is built
  • Allows you to reference any resource included in the res/ folder
  • Contains static subclasses for each of the resources available
    • such as R.string and R.drawable
  • Each subclass in R exposes its associated resources as variables
    • These are integers representing locations in the resource table
    • NOT instances of the resource
  • Built in classes handle actually getting the instances required
  • Android system resources are accessed in a similar manner, using:
    • android.R
24
Q

Using Resources in Code:

Resource class

A

The Resource class represents the resource table within the application.

  • Includes helper methods to extract actual instances of a resource when needed
  • To access the Resources instance of the app, use the getResources() method on the application context:
    • Resources myRes = getResources()
  • Can now get instances:
    • String myStr = myRes.getText(R.strings.my_str);
  • For backwards compatibility, use the ResourcesCompat class
25
Q

Referencing Resources

within Resources

A

Use the @ notation, with syntax:

attribute=”@[package:]resourceType/resourceId”

  • Example:
    android: textColor=”@color/myColor”
  • The package is only needed if a resource from another package, such as the “android” package, is needed
  • This is particularly useful for layouts and styles, letting you create specialized variations on themes and localized strings and image assets
  • Also useful for supporting different images and spacing for a layout
26
Q

Referencing Resources

withing Resources:

Referring to Styles in the Current Theme

A

Android provides a shortcut to allow you to use styles that are defined in the current Theme

Use “?android:” rather than “@” as a prefix to the resource you want to use.

Example:

android: textColor=”?android:textColor”
* Instead of referencing a particular attribute wherever that color needs to be used, it is defined once within a View or Container

27
Q

Using

Alternative Resource Values:

Overview

A

Can create different resource values for specific languages, locations and hardware configurations.

  • Use a parallel directory structure within the res folder
  • A hyphen is used to separate qualifiers that specify conditions you are providing alternatives for:
    • res/values (default)
    • res/values-fr (French Language)
    • res/values-fr-rCA (French Canadian location)
  • At runtime, Android chooses among these values depending on the current device
28
Q

Using

Alternative Resource Values:

List of Qualifiers (23)

A
  • Internationalization:
    • Mobile Country Code (MCC)
    • Mobile Network Code (MNC)
    • Language
    • Region
    • Layout Direction
  • Screen Physical Dimensions:
    • Smallest Screen Width
    • Available Screen Width
    • Available Screen Height
    • Screen Size
    • Screen Aspect Ratio
    • Screen Shape
  • Screen Capabilities:
    • Screen Color Gamut
    • Screen Dynamic Range
    • Screen Pixel Density
  • Preferences:
    • Screen Orientation
    • UI Mode
    • Night Mode
  • Hardware:
    • Touchscreen Type
    • Keyboard Availability
    • Keyboard Input Type
    • Navigation Key Availability
    • UI Navigation Type
  • Platform Version
29
Q

Using

Alternative Resource Values:

Qualifier Categories:

Internationalization

A
  • Mobile Country Code (MCC)
  • Mobile Network Code (MNC)
  • Language
  • Region
  • Layout Direction
30
Q

Using

Alternative Resource Values:

Qualifier Categories:

Screen Physical Dimensions

A
  • Smallest Screen Width
  • Available Screen Width
  • Available Screen Height
  • Screen Size
  • Screen Aspect Ratio
  • Screen Shape
31
Q

Using

Alternative Resource Values:

Qualifier Categories:

Screen Capabilities

A
  • Screen Color Gamut
  • Screen Dynamic Range
  • Screen Pixel Density
  • Touchscreen Type
32
Q

Using

Alternative Resource Values:

Qualifier Categories:

Preferences/Accessibility

A
  • Screen Orientation
  • UI Mode
  • Night Mode
33
Q

Using

Alternative Resource Values:

Qualifier Categories:

Hardware

A
  • Touchscreen Type
  • Keyboard Availability
  • Keyboard Input Type
  • Navigation Key Availability
  • UI Navigation Type
34
Q

Using

Alternative Resource Values:

Screen Pixel Density:

Densities to Support

and

Associated Qualifiers

A

Screen Pixel Densities:

(in dots per inch: dpi)

Drawable resources should be supplied for each density

  • Low - 120dpi
    • ldpi
  • Medium - 160 dpi
    • mdpi
  • High - 240 dpi
    • hdpi
  • Extra High - 320 dpi
    • xhdpi
  • Extra Extra High - 480 dpi
    • xxhdpi
35
Q

Runtime Configuration Changes:

Overview

A
  • Occasionally, a configuration might change while an App is running
    • Especially screen orientation
  • Android handles this by terminating and restarting the active Activity
    • This forces resource resolution to be reevaluated for the Activity to fit the new configuration
  • This default behavior can be overridden:
    • add an ‘android:configChanges’ attribute to the activity’s node in the manifest
    • Override the event handler ‘onConfigurationChanged()’ to handle the changes
36
Q

Runtime Configuration Changes:

android:configChanges

values that can be specified

A
  • mcc
  • mnc
  • locale
  • keyboardHidden
  • keyboard
  • fontScale
  • layoutDirection
  • uiMode
  • orientation
  • screenLayout
  • screenSize
  • smallestScreenSize