Android Tips, Techniques and Theory

PART 1: Creating Content with XML with Android Studio (No programming required)

With Simple Recipes

Go Back To TOC


XML Resources

Simple XML resource files (e.g., string, color and dimension) are defined in the /res/values directory. These resource files use special XML elements with name/value pairs and are compiled in the app at build time and can be edited with the Resource Editor or you can double-click on one of them and edit them directly.

To create a new XML file for these simply resources, select File > New > XML > Values in XML files and then specify the type of resource file you want to create (e.g., string_en).

Now, to get a REFERENCE from another compiled resource (e.g., layout file) use the syntax: @[resource type] / [resource name]. For example, you could write code similar to this to retrieve a string resource:

@string/helloWorld

TIP: To easily convert a literal string to a string resource:

Application String Resources

Use string resources anywhere an app needs to display text that can be used to make the app multi-lingual. It is highly recommended that ALL string constants be stored in the strings.xml file so that they can be localized to other languages. All you need to do is make a copy of the entire values directory and modify the content of the strings to another language in each of the folder with a qualifier name (e.g., values-fr).

TIP: It is best practice to define your string resource constants upfront based on what text you have in your app that you want to localize. You may ask yourself the question what should be made into a string resource. Anything TEXT you:

EXAMPE:

<resources>
    <
string name="app_name">HelloWorld</string>
    <
string name="action_settings">Settings</string>
</
resources>

NOTE: The name of the resource is typically the PURPOSE of the string (e.g., app_name, dialog_title, toast_message, cancel_button, delete_button).

String resources names must have well-formatted values. String resources that contain apostrophes or single quotes must be escaped with backward slashes (\) or wrapped in double quotes.

EXAMPLES:

EXAMPLE 1:

To apply the string using XML, select the component you want to apply it to and add the following attributes in the Properties panel or write it manually in the XML file:

Text: “Hello World”–if no string resource was created or
Text: @string/app_name
–if a string resource was created

NOTE: @string/app_name where @string is the resource TYPE and the app_name is the resource ID. The actual value is in the resource file if press the CTRL and then click on the string, it will open the resource file and place the cursor on that line so that you can see the value.

Like ids, string resources are defined in the R.java file (e.g., @strings/componentID. String literal are not recommended if you plan to publish your app for multiple languages.

System String Resources

Like Application resources, you can also use System or Project resources. The major difference between an application and system resource is the addition of android: to the resource type and a system name. This is available for all types of system resources (e.g., ID, drawable, strings, layouts, etc.).

EXAMPLE 2: To get a REFERENCE from an Android resource use the syntax: @android: [resource type] / [resource name]. For example, you could write code similar to this to retrieve a system string ok:

@android:string/ok

NOTE: As you add more files and folder to your app, Android Studio will automatically generate a class for EACH resource type in the R.java file:

package com.examples.HelloWorld
publish final class R {
publish state final class attr {
}
publish static final class drawable {
publish static final int ic_launcher=0x7f0200000;
}
publish static final class layout {
publish static final int main=0x7f0300000;
}
publish static final class string {
publish static final int app_name=0x7f0400000;
publish static final int hello=0x7f0500001;
}

NOTE: If you delete the R.java file, Android Studio will regenerate it again if the project does not contain any errors.

EXAMPLE: How to Manage Multiple Languages

You can use a default resource file to create resource file for another language format.

Color Resources

Use color resources for app’s screen appearance in the colors.xml file.

EXAMPE:

<resources>
    <
string name="background_color">#00FF00</string>
    <
string name="text_color">#FF0000</string>
</
resources>
 
The Android system supports both 12 and 24 bit RGB colors with alpha.

To apply the color using XML, select the component you want to apply it to and add the following attributes in the Properties panel or write it manually in the XML file:

TextColor:#ff0000–if no color resource was created or
TextColor:@color/text_color
–if a color resource was created

Dimension Resources

Use dimension resources to specify size of UI elements (e.g., Button, ImageView).

EXAMPE:

<resources>
    <
string name="thumbNail">100dp</string>
</
resources>
 
Dimension resource value must end with a unit of measurement (e.g., px, dp, sp).

Drawable Resources

Use drawable for bitmap (e.g., *.png) images or ShapeDrawable.

TIP: You can drag-and-drop an image onto the drawable folder. Remember that image must be lowercase and contain letters, numbers and underscores.

Image formats supported are:

Because you typically create graphics for multiple screen sizes, you typically need to create graphics for various screen size and screen densities. You will need to place the graphics in appropriate directories with suffix qualifiers (e.g., hdpi, ldpi, mdpi, xhdpi) but with the same image name. The Android Resource Manager is responsible for “managing” and selecting the correct resource depending on the device size or pixel density and displays it on the screen. Each qualifier MAPS to a particular screen denisity:

NOTE: The ic_launcher.png icon is automatically created for you and placed in the appropriate directories. However, you can replace them with your own custom launcher cions.

To apply the drawable using XML, select the component you want to apply it to and add the following attributes in the Properties panel or write it manually in the XML file:

src:ic_launcher–if no color resource was created or
src:@drawable/ic_launcher
–if a color resource was created

EXAMPLE: How To Tile A Bitmap

There are times when you need to tile an image (i.e., repeat an image horizontal or vertical) within a component’s background so that it can create a tiled pattern. Below are the steps on how to do this:

EXAMPLE: Compound Drawables

There are times when you want to combine an image with text (e.g., icon with text below it). However, instead of using an ImageView AND a TextView, you can used just and TextView with a drawable propert(ies) to set both. NOTE: There are some cases, where you will need both a TextView and an ImageView.

Layout Resources

App’s entire or partial screens are defined using specialized XML files called layouts. They can be created and previewed using the Resource Editor (discuss next) or their XML file can be edit directly. Each element in an XML file maps to a Java class from the SDK or a support library.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.Main2Activity"
tools:showIn="@layout/activity_main2"
android:orientation="vertical">

<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
</
LinearLayout>

TIP:
When you have two layout files (e.g., activity_main.xml and content_main.xml) that defines an activity, it is helpful to think of the them as template-like framework. For example, you could think of the activity_main as the area of the template that does NOT changes (e.g., actionBar) and the content_main.xml file as the area that  you can use to add components (e.g., the white area that you can see in design view).

File Resources

Android can also include any files as a resource. However, the XML file format is well-suited for various types of file formats for structured data:

Raw Resources

An app can also contain raw files (e.g., audio/video).