Professional Documents
Culture Documents
Unit 2: Managing Application Resources: - Resources - Working With Resources. - Referencing The System Resources
Unit 2: Managing Application Resources: - Resources - Working With Resources. - Referencing The System Resources
• Ex:
boolean bAdvancedMode getResources().getBoolean(R.bool.bAdvancedFeaturesEnabled);
Working with Integer Resources
Defining Integer Resources in XML
Integer values are appropriately tagged with the
<integer> tag and represent a namevalue pair.
• To use the integer resource, must load it using the Resource class.The
following code
• accesses application’s integer resource named numTimesToRepeat:
And then to define a localizable string-array that references the string resources by
name in the /res/arrays.xml file like this:
<?xml version ”1.0” encoding ”utf-8”?>
<resources>
<string-array name ”soups”>
<item>@string/minestrone_soup</item>
<item>@string/chowder_soup</item>
<item>@string/chicken_soup</item>
</string-array>
</resources>
Working with Layouts
• A layout XML resource is where many different
resources come together to form the definition of an
Android application screen.
• Layout resource files are included in the /res/layout/
directory and are compiled into the application
package at build time.
• Layout files might include many user interface
controls and define the layout for an entire screen or
describe custom controls used in other layouts.
• Here’s a simple example of a layout file
(/res/layout/main.xml) that sets the screen’s
background color and displays some text in the
middle of the screen
• The main.xml layout file that displays this screen
references a number of other resources, including
colors, strings, and dimension values, all of which
were defined in the strings.xml, colors.xml, and
dimens.xml resource files.
• The color resource for the screen background color and resources for a
TextView control’s color, string, and text size follows:
<?xml version ”1.0” encoding ”utf-8”?>
<LinearLayout xmlns:android
“http://schemas.android.com/apk/res/android”
android:orientation= ”vertical” android:layout_width ”fill_parent”
android:layout_height= ”fill_parent”
android:background =”@color/background_color”>
<TextView
android:id =”@+id/TextView01”
android:layout_width= ”fill_parent”
android:layout_height= ”fill_parent”
android:text =”@string/test_string”
android:textColor= ”@color/text_color”
android:gravity =”center”
android:textSize =”@dimen/text_size”></TextView>
</LinearLayout>
• The following code retrieves the main.xml layout file for XML parsing:
XmlResourceParser myMainXml
getResources().getLayout(R.layout.main);
Working with Styles
• Styles are tagged with the <style> tag and should be stored in the /res/values/
directory.
• Style resources are defined in XML and compiled into the application binary at build
time.
• Here’s an example of a simple style resource file /res/values/styles.xml containing
two styles: one for mandatory form fields, and one for optional form fields on
TextView and EditText objects:
<?xml version ”1.0” encoding ”utf-8”?>
<resources>
<style name= ”mandatory_text_field_style”>
<item name =”android:textColor”>#000000</item>
<item name =”android:textSize”>14pt</item>
<item name =”android:textStyle”>bold</item>
</style>
<style name =”optional_text_field_style”>
<item name =”android:textColor”>#0F0F0F</item>
<item name =”android:textSize”>12pt</item>
<item name =”android:textStyle”>italic</item>
</style>
</resources>
Working with Themes
• Themes are much like styles, but instead of being
applied to one layout element at a time, they are
applied to all elements of a given activity (which,
generally speaking, means one screen).
• Themes are defined in exactly the same way as styles.
• Themes use the <style> tag and should be stored in
the /res/values directory.
• The only difference is that instead of applying
that named style to a layout element, define it as the
theme attribute of an activity in the
AndroidManifest.xml file.
Referencing System Resources
• The android package contains all kinds of resources,
which can be browsed by looking in the android.R
subclasses.
• system resources for:
1. Animation sequences for fading in and out
2. Arrays of email/phone types (home, work, and
such)
3. Standard system colors
4. Dimensions for application thumbnails and icons
5. Many commonly used drawable and layout types
6. Error strings and standard button text
7. System styles and themes
Creating Contextual Links in Text
• Text contains references to email addresses, web pages, phone numbers, or even street
addresses – supported by using the attribute autoLink
• The autoLink attribute has four values for use in combination with each other
• When enabled, these autoLink attribute values create standard web-style links to the
application that can act on that data type.
• Text can contain the following values for the autoLink attribute:
• none: Disables all linking.
• web: Enables linking of URLs to web pages.
• email: Enables linking of email addresses to the mail client with the recipient filled.
• phone: Enables linking of phone numbers to the dialer application with the phone
number filled out, ready to be dialed.
• map: Enables linking of street addresses to the map application to show the location.
• all: Enables all types of linking.
Example for Contextual Links
<TextView
android:id ”@+id/TextView02”
android:layout_width ”wrap_content”
android:layout_height ”wrap_content”
android:text ”@string/autolink_test”
android:autoLink ”web|email” />
Helping the User with Auto Completion
• way to help the user with entering commonly used data into
forms.This
• functionality is provided through the auto-complete feature.
• There are two forms of auto-complete.
• standard style of filling in the entire text entry based on what the user
types.
• If the user begins typing a string that matches a word in a developer-
provided list,
• the user can choose to complete the word with just a tap.
• This is done through the AutoCompleteTextView control
• The second method allows the user to enter a list of items, each of
which has autocomplete Functionality.
• These items must be separated in some way by providing a Tokenizer
to the MultiAutoCompleteTextView object that handles this method.
• A common Tokenizer implementation is provided for comma-
separated lists
• Its is used by specifying the
MultiAutoCompleteTextView.CommaTokenizer object.
Example:
final String[] COLORS {
“red”, “green”, “orange”, “blue”, “purple”, Here is the layout resource definition for this
AutoCompleteTextView control:
“black”, “yellow”, “cyan”, “magenta” };
ArrayAdapter<String> adapter <AutoCompleteTextView
new ArrayAdapter<String>(this, android:id ”@+id/AutoCompleteTextView01”
android.R.layout.simple_dropdown_item_1li android:layout_width ”fill_parent”
ne, android:layout_height ”wrap_content”
COLORS); android:completionHint ”Pick a color or type
your own”
AutoCompleteTextView text
(AutoCompleteTextView) android:completionThreshold ”1” />
findViewById(R.id.AutoCompleteTextView01)
;
text.setAdapter(adapter);
Giving Users Input Choices Using Spinner
Controls
• Helps to limit the choices available for users to type.
Example:
final Spinner spin (Spinner) findViewById(R.id.Spinner01);
TextView text_sel (TextView)spin. getSelectedView();
String selected_text text_sel.getText();
Using Indicators to Display Data to Users
• Indicating Progress with ProgressBar
• Adjusting Progress with SeekBar
• Displaying Rating Data with RatingBar
• Showing Time Passage with the Chronometer