Resources • Binding to an application – Create child directories under the res folder – Store files or XML specifications in the child directories • Examples – Symbolic constants for primitives and arrays – Icons, Pictures, audio, and video – Drawable and style components – UI layouts – XML file specifications – Menus and Layouts efinition: A value or a file bound to an applicatio
Style Sheets (CSS). Separate structure (HTML tags) from browser presentation Control a site’s look and feel Three types Inline : apply to a single HTML tag Document level : apply to groups of a document’s tags External : apply to groups of tags in a set of web site documents - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Resources
• Binding to an application– Create child directories under the res folder– Store files or XML specifications in the child directories
• Examples– Symbolic constants for primitives and arrays– Icons, Pictures, audio, and video– Drawable and style components– UI layouts– XML file specifications– Menus and Layouts
Definition: A value or a file bound to an application
– Platform versions: -v3, -v2, -v4, etc.– Navigation: -trackball, -wheel, -stylus, -finger, etc. – Keyboard: -12key, -querty, -nokeys– Screen density: -ldpi, -mdpi,-hdpi,-xdpi– Orientation: -land, -port– Screen size: -small, -normal, -large, -xlarge– Screen height or width: -w720dp, -h1024dp, etc. – Language and/or region: -en, -fr, -en-US, -fr, etc.
• Examples (Note: for complex specifications, the order is important)– values-fr for Symbolic constants in french– layout-landscape– Complex resource specification: drawable-fr-land-xlarge-v4
Syntax: <reservedName>[ [-<modifier>] … ]*
res/values Subdirectories–The default subdirectory has no modifiers–If a resource does not exist in a subdirectory with modifiers, Android uses the default–Without a default, program crashes when encountering an unexpected device configuration–Subdirectories with modifiers override the defaults–Modifiers must be added in a precice order
Modifier order
1. Country code (mcc310) mcc = mobile country code
2. Language and region (en, fr, en-rUS, …)3. Layout direction (ldrtl, ldltr)4. Smallest width (sw320dp, …)5. Availble width (w1024dp, …)6. Available height (h720dp, …)7. Screen size (small, normal, large,
+ means create symbolic constant if not already defined
fill_parent deprecated replaced with match_parent after version 2.0
Java access: setContentView(R.layout.main); // R.layout.file_namemText = (EditText) findViewById(R.id.note); // Get the widget
Symbolic Constants
• Purpose– Localize the values outside the source code– Enables easy changes without modifying code– Enables adaptations for different localities and
configurations, without maintaining multiple loads
• Examples– res/values-en contains strings in English; res/values-fr
contains strings translated to French– res/values-sp contains color combinations that are
appropriate to Spanish culture, like yellow for emphasis rather than red
Defined with XML files in one of the res/values subdirectories
Defining Symbolic Constants
• How? Create XML files in the res/values subdirectories
• Operation– Android gathers the XML files and merges their contents– Each build automatically generates the file R.java
• Stored in a gen subdirectory (gen/com.example.android.notes)• Contains integer codes corresponding to each symbol
– Application code addresses these codes using R.<type>.name– XML files references existing codes using @<type>/name– All modifier subfolders should use the same type and name
values so the code device and location independent
String Constants• Typically defined in strings.xml
• Dimensions: Typically dimens.xml; Units of measure are px, in, mm, pt, dp, sp– Declaration: <dimension name="border">5px</dimen>– Java: activity.getResources.getDimension(R.dimen.border);
• Attributes: Typically attrs.xml; Customize the application based on parameters– Declaration: <declare-styleable name="styles">
<scale android:fromXScale="0" android:toXScale="1"android:fromYScale="0.1" android:toYScale="1.0"android:duration="5000" android:pivotX="50%" android:pivotY="50%” // Zoom from the
centerandroid:startOffset="1000"
/></set>
• Access from Java: R.anim.foo where foo.xml is the file in res/anim
Create flashy animations on an Android device
Purpose: vary scale factors, rotations, or colors from a starting to ending value over a period of time
Formatting and Styling• Formatting Strings (format is like using the C sprintf method)
XML declaration: <string name="hi">Hello, %1$s!</string>Java usage: String text = String.format(context.getString(R.string.hi), user);
• Styling a String
XML declaration: <string name="bi"> <b><i>bold and italic</b></i> </string>Java usage:textView.setText(Html.fromHtml(R.string.bi),
TextView.BufferType.SPANNABLE);
• Styling part of a StringXML declaration: <string name="ital">< i>Italics</i> to emphasize</string>Note: <b>, <i>, <u>, <sup>, <sub>, <strike>,<big>, <small>, <monospace> are ok
Java usage: Spannable text = (Spannable)view.getText();TextAppearanceSpan style = new TextAppearanceSpan(context, R.style.ital); text.setSpan(style, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Note: applies style from characters 0 to 5 excluding end points
File Resources• Images: Drop in a res/drawable subfolder; Codecs: jpg, gif, png
• XML files to parse: Drop in a res/xml subfolder• Arbitrary files: Drop in a res/raw subfolder or /assets
– Most files are pre-compiled during the build. These files are not– res/raw files have an R.java ID. For example R.raw.foo without an
extension for foo.png or foo.txt. This approach requires lower case alphanumeric characters and periods.
getResources().openRawResource(R.raw.myfilename).– Files stored in /assets do not have an R.java ID. Access using Java
input stream programming using a fully qualified path name.InputStream is = getAssets().open("myFolder/somefile.txt");
– Examples of use: Video files or XML files that the application parses using Java DOM or SAX classes
Optional Presentation
Implementation of the class project
1. Describe how it works and give a demo2. Use an actual device if possible3. Specifically discuss innovations that you implemented4. Discuss possible improvements that focus on making the
application more exciting for users.5. Discuss the advantages or disadvantages of using JavaScript
or native Android applications, your preferences, and the reasoning behind your position.