Android Module 2 Part 1 Calicut University PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Android Programming

MODULE TWO

www.teachics.org
The Computer Science Learning Platform.

2
■ Understanding android resources - String
resources, Layout resources, Resource
reference syntax.
Table of Contents ■ Defining own resource IDs - Enumerating
key android resources,string arrays, plurals,
Colour resources, dimension resources,
image resources.

3
Android Resources
■ A resource in Android is a file or a value that is bound to an executable application.
■ You can change them or provide alternatives without recompiling the application.
■ Examples - strings, colors, bitmaps, and layouts.
■ You should place each type of resource in a specific subdirectory of your project's res/
directory.
■ Resource directories supported inside project res/ directory are,
● animator/
● color/
● drawable/
● mipmap/
● layout/
● menu/
● raw/
● values/ - arrays.xml, colors.xml, dimens.xml, strings.xml, styles.xml
● xml/
● font/

teachics.org 4
String Resources
■ A string resource provides text strings for your application with optional text styling and
formatting.
■ Three types
● String - XML resource that provides a single string.
● String Array - XML resource that provides an array of strings.
● Quantity Strings (Plurals) - XML resource that carries different strings for
pluralization.

teachics.org 5
String
■ A single string that can be referenced from the application or from other resource files.
■ File location - res/values/strings.xml
■ Syntax

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="string_name">text_string</string>
</resources>

■ Example

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="hello">Hello!</string>
</resources>

■ Applying String to a view - android:text="@string/hello"


■ Retrieve string in code - android:text="@string/hello"

teachics.org 6
String Arrays
■ An array of strings that can be referenced from the application.
■ File location - res/values/strings.xml
■ Syntax Example
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string-array name="string_array_name"> <string-array name="planets_array">
<item>text_string</item> <item>Mercury</item>
</string-array> <item>Venus</item>
</resources> <item>Earth</item>
<item>Mars</item>
</string-array>
</resources>

■ Retrieve string arrays in code


Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

teachics.org 7
Plurals (Quantity Strings)
■ The resource plurals is a set of strings.
■ These strings are various ways of expressing a numerical quantity, such as how many pages
are there in a book.
● There is 1 egg.
● There are 2 eggs.
● There are 0 eggs.
■ Full set supported by android is zero, one, two, few, many and other.
■ Syntax

<?xml version="1.0" encoding="utf-8"?>


<resources>
<plurals name="plural_name">
<item
quantity=["zero" | "one" | "two" | "few" | "many" | "other"]>text_string</item>
</plurals>
</resources>

teachics.org 8
Plurals (Quantity Strings)
■ File location - res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<plurals name="numberOfSongsAvailable">
<item quantity="one">%d song found.</item>
<item quantity="other">%d songs found.</item>
</plurals>
</resources>

■ Usage
int count = getNumberOfSongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);

teachics.org 9
String Formatting
When a string contains characters that have special usage in XML or Android, you must escape the
characters.
■ @ \@
■ ? \?
■ < &lt;
■ & &amp;
■ Single quote (')
● &apos;
● \'
● Enclose the entire string in double quotes ("This'll work", for example)
■ Double quote (")
● &quot;
● \"

teachics.org 10
String Formatting
You can add styling to your strings with HTML markup.

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="welcome">Welcome to <b>Android</b>!</string>
</resources>

teachics.org 11
Layout Resource
■ A layout resource defines the architecture for the UI in an Activity or a component of a UI.
■ File location - res/layout/filename.xml
■ Syntax

<?xml version="1.0" encoding="utf-8"?>


<ViewGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@[+][package:]id/resource_name"
android:layout_height=["dimension" | "match_parent" | "wrap_content"]
android:layout_width=["dimension" | "match_parent" | "wrap_content"]
[ViewGroup-specific attributes] >
<View
android:id="@[+][package:]id/resource_name"
android:layout_height=["dimension" | "match_parent" | "wrap_content"]
android:layout_width=["dimension" | "match_parent" | "wrap_content"]
[View-specific attributes] >
</View>
<ViewGroup >

teachics.org 12
Layout Resource
■ <ViewGroup> - A container for other View elements. Different kinds of ViewGroup objects
include LinearLayout, RelativeLayout, and FrameLayout.
■ <View> - An individual UI component, generally referred to as a "widget". Different kinds of View
objects include TextView, Button, and CheckBox.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout

teachics.org 13
Color Resource
■ A color value defined in XML.
■ The color is specified with an RGB value and alpha channel.
■ File location - res/values/colors.xml
■ The value always begins with a # character and then followed by the Alpha-Red-Green-Blue
information in one of the following formats:
● #RGB
● #ARGB
● #RRGGBB
● #AARRGGBB
■ Syntax

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="color_name">hex_color</color>
</resources>

teachics.org 14
Color Resource
■ File location - res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>

■ Usage
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
■ This layout XML applies the color to an attribute:

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/translucent_red"
android:text="Hello"/>

teachics.org 15
Dimension Resource
■ A dimension value defined in XML.
■ A dimension is specified with a number followed by a unit of measure (10px, 2in, 5sp).
■ The following units of measure are supported by Android:
● in - inches.
● mm - Millimeters.
● px - pixels (Corresponds to actual pixel on the screen)
● pt - Points (1/72 of an inch based on the physical size of the screen)
● dp - Density-independent Pixels - pixels based on a 160dpi (pixel density per inch) screen
(dimensions adjust to screen density)
● sp - Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size
preference.
■ File location - res/values/dimens.xml

teachics.org 16
Dimension Resource
■ Syntax
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dimension_name">dimension</dimen>
</resources>

■ Example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>

■ This application code retrieves a dimension


Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
■ This layout XML applies dimensions to attributes
android:textSize="@dimen/font_size"

teachics.org 17
Image Resource
■ A drawable resource is a general concept for a graphic that can be drawn to the screen.
■ Android supports bitmap files in three formats: .png (preferred), .jpg (acceptable), .gif
(discouraged).
■ Android creates a Drawable resource for any of these files when you save them in the
res/drawable/ directory.
■ An image is saved at res/drawable/myimage.png.
This layout XML applies the image to a View:
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/myimage" />

■ The code that retrieves the image as a Drawable:


Resources res = getResources();
Drawable drawable = ResourcesCompat.getDrawable(res, R.drawable.myimage, null);

teachics.org 18
Resource Reference Syntax
■ Regardless of the type of resource, all Android resources are identified (or referenced) by their
IDs in Java source code.
■ The syntax that is used to allocate an ID to a resource in the XML file is called resource
reference syntax.
■ All resource IDs are defined in your project's R class, which the aapt tool automatically
generates.
■ This resource reference has the following formal structure:
@[<package_name>:]<resource_type>/<resource_name>
■ <package_name> is the name of the package in which the resource is located (not required
when referencing resources from the same package).
■ <resource_type> is the R subclass for the resource type.(drawable, id, layout, array, etc.)
■ <resource_name> is either the resource filename without the extension or the android:name
attribute value in the XML element (for simple values).

teachics.org 19
Defining Own Resource IDs
■ The general pattern for allocating an ID is either to create a new one or to use the one created
by the Android package.
■ However, it is possible to create IDs beforehand and use them later in your own packages
■ The line <TextView android:id="@+id/text"/> indicates that an ID named text is used if it
already exists.
■ If the ID doesn’t exist, a new one is created.
■ Predefining an ID

<?xml version="1.0" encoding="utf-8"?>


<resources>
<item type="id" name="id_name"/>
</resources>

■ Reusing a Predefined ID
<Button android:id="@id/button_ok"/>

teachics.org 20
Enumerating Key Android Resources

teachics.org 21
Thank You.

The Computer Science Learning Platform.

22
References
■ Komatineni, S. and MacLean, D., 2012. Pro Android 4. New York: Apress L.P.
■ Android Developers. App Resources Overview. [online]
Available at: <https://developer.android.com/guide/topics/resources/providing-resources>

teachics.org 23

You might also like