Managing Application Resources Chapter 7 Resources Wellwritten applications

  • Slides: 14
Download presentation
Managing Application Resources Chapter 7

Managing Application Resources Chapter 7

Resources • Well-written applications access resources programmatically rather than hard-coding them in the source

Resources • Well-written applications access resources programmatically rather than hard-coding them in the source code • Android applications are composed of two things • Functionality – code instructions; determines how your application behaves • Data – resources; includes text strings, images, icons, audio files, videos, etc. • Android resource files are stored separately from the. java class files in a project • Common resources are stored in XML

Resources • Resources are organized in strict directory hierarchy • All resources are stored

Resources • Resources are organized in strict directory hierarchy • All resources are stored under the /res project directory in specially names subdirectories that must be lowercase • Different resources are stored in different folders • The resource subdirectories generated when you create an Android projects are shown below Resource Subdirecotry Purpose /res/drawable-*/ Graphics resources /res/layout/ User interface resources /res/values/ Simple data such as strings and color values

Resources • Resources can be organized in even more specially named directories • /res/drawable-hdpi

Resources • Resources can be organized in even more specially named directories • /res/drawable-hdpi • /res/drawable-lpdi • /res/drawable-mdpi • Eclipse with the Android Development Tools (ADT) makes adding resource simple • Resources are compiles, resulting in the generation of the R. java source file • Enables access to resources programmatically

Resources • All resource filenames must be lower-case and simple • Letters, numbers, and

Resources • All resource filenames must be lower-case and simple • Letters, numbers, and underscores only Resource Type Required Directory Suggested Filenames XML Tag Strings /res/values/ strings. xml <string> String Pluralization /res/values/ strings. xml <plurals>, <items> Arrays of Strings /res/values/ strings. xml or arrays. xml <string-array>, <item> Booleans /res/values/ bools. xml <bool> Colors /res/values/ colors. xml <color> Color State Lists /res/color/ buttonstates. xml, indicators. xml <selector>, <item>

Resources Resource Type Required Directory Suggested Filenames XML Tag Dimensions /res/values/ dimens. xml <dimen>

Resources Resource Type Required Directory Suggested Filenames XML Tag Dimensions /res/values/ dimens. xml <dimen> Integers /res/values/ integers. xml <integer> Arrays of Integers /res/values/ integers. xml <integer-array> Mixed-Type Arrays /res/values/ arrays. xml <array>, <items> Simple Drawables (Paintable) /res/values/ drawables. xml <drawable> Graphics /res/drawable/ icon. png, logo. jpg Supported graphics files or drawable definition SML files Tweened Animations /res/anim/ fadesequence. xml, spensequence. xml <set>, <alpha>, <scale>, <translate>, <rotate>

Resources Resource Type Required Directory Suggested Filenames XML Tag Property Animations /res/animator/ mypropanims. xml

Resources Resource Type Required Directory Suggested Filenames XML Tag Property Animations /res/animator/ mypropanims. xml <set>, <object. Animator>, < value. Animator> Frame-by-Frame Animations /res/drawable/ sequence 1. xml, sequence 2. xml <animation-list>, <item> Menus /res/menu/ mainmenu. xml, helpmenu. xml <menu> XML Files /res/xml/ data. xml, data 2. xml Defined by the developer

Resources Resource Type Required Directory Suggested Filenames XML Tag Raw Files /res/raw/ jingle. mp

Resources Resource Type Required Directory Suggested Filenames XML Tag Raw Files /res/raw/ jingle. mp 3, somevideo. mp 4, helptext. txt Defined by the developer Layouts /res/layout/ main. xml, help. xml Varies. Must be a layout control styles. xml, themes. xml <style> Styles and Themes /res/values/ New resource types are added frequently to Android SDK.

Resources • Storing Primitive Resource Types • Simple resource types like strings, colors, and

Resources • Storing Primitive Resource Types • Simple resource types like strings, colors, and dimensions are stored under /res/values folder in XML files • Each resource file under /res/values folder should begin with following header: <? xml version=“ 1. 0” encoding=“utf-8”? > • Followed by root node <resources> • Followed by the specific resource element types like <string> or <color> • Each resource is defined using a different element name

Resources <? xml version=“ 1. 0” encoding=“utf-8”? > <resources> <string name=“app_name”>Use Some Resources</string> <string

Resources <? xml version=“ 1. 0” encoding=“utf-8”? > <resources> <string name=“app_name”>Use Some Resources</string> <string name=“hello”>Hello World, Use Some Resources</string> <color name=“pretty. Text. Color”>#ff 0000</color> <dimen name=“text. Point. Size”>14 pt</dimen> <drawable name=“red. Drawable”>#F 00</drawable> <string-array name=“flavors”> <item>Vanilla</item> <item>Chocolate</item> <item>Strawberry</item> </string-array> </resources>

Resources • Android platform has robust mechanism for loading appropriate resources at runtime •

Resources • Android platform has robust mechanism for loading appropriate resources at runtime • Useful to think of the resources stored at the directory hierarchy as the application’s default resources • Developer can supply special versions of resources to load instead of defaults under certain conditions • Specialized resources are called alternative resources • Common reasons to use alternative resources are: • Internationalization • Localization • Design application that runs smoothly on different device screens & orientations

Resources - Color • Android applications can store RGB color values • You can

Resources - Color • Android applications can store RGB color values • You can use the color values to set the color of text or backgrounds. • RGB color values always start with the hash symbol # • The alpha value can be given for transparency control • Following color formats are supported: • • #RGB – example is #F 00 for 12 -bit color red #ARGB – example is #8 F 00 for 12 -bit color read with alpha 50% #RRGGBB – example is #FF 00 FF 24 -bit color magenta #AARRGGBB – example is #80 FF 00 FF 24 -bit color magenta with alpha 50% • Color values are tagged with <color> tag in a name/value pair.

Resources - Dimensions • Many user interface layout controls like text controls and buttons

Resources - Dimensions • Many user interface layout controls like text controls and buttons are drawn to specific dimensions • Dimensions can be stored as resources Unit of Measurement Description Resource Tag Required Example Pixels Actual screen pixels px 20 px Inches Physical measurement in 5 in Millimeters Physical measurement mm 2 mm Points Common font measurement unit pt 14 pt Screen density – independent pixels Pixels relative to 160 dpi screen (preferable dimension for screen compatibility) dp 14 dp Scale-independent pixels Best for scalable font display sp 14 sp