Tuesday, November 9, 2010

Android #4: Working with Android Resources - String Resources

Typically the res folder within any android project contains all external resources such as icons, images, layout, string etc under the respective sub-folders within. All resources are bound to the app in a way that we can change them without the need to recompile the app or change any app code. Unlike assets, android generates an ID in R.java file (auto-generated file) for each resource file using which they will be accessed within the code. Resources are very much useful when developing multi-lingual app supporting l10n and i18n.

Types of resources:
  • Strings, colors, arrays, dimensions are defined in res/values/ directory. Typical usage would be in Localization and Internationalization
  • Images are defined in res/drawable directory. Typical usage would be to put all the images or icons that an application needs
  • XML’s are defined in res/xml/ directory for xml files. Typical usage would be to hold custom data that can be used in application
  • Layout resources are defined in res/layout/ for declaring the views. Typical usage would be to construct the user interface of the activities
Following example illustrates accessing resources from res folder in a simple android project:



Working with String resources:

Android offers three types of string resources: Plain Strings, string formats and styled texts. Refer the following examples

layout/main.xml:
<?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">

<!-- ResourceDemo -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/resPlainStrTxt"
android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Plain String Resources - This is referenced from res/layout/main.xml"
android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:id="@+id/resPlainStrTxtId"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:id="@+id/resFormattedStrTxtId"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/resFormattedStrTxt" android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:id="@+id/resStyledStrTxtId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textColor="#CDE472" android:text="@string/resStyledStrTxt"/>

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

</LinearLayout>
ResourceDemoActivity.java
package in.satworks.android.samples.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ResourceDemoActivity extends Activity {
private TextView resPlainStrTxtView=null;
private TextView resFormattedStrTxtView=null;
private TextView resStyledStrTxtView=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Working with resources - Start
// Accessing Resources for Plain String contents from the resources folder - start
resPlainStrTxtView=(TextView)findViewById(R.id.resPlainStrTxtId);
resPlainStrTxtView.setText("Plain String Resources - This is referenced from code");
// Accessing Resources for Plain String contents from the resources folder - end

// Accessing Resources for Formatted String contents from the resources folder - start
resFormattedStrTxtView=(TextView)findViewById(R.id.resFormattedStrTxtId);
String formatStr=getString(R.string.resFormattedStrTxt);
String formattedStr=String.format(formatStr, "- \"This New Formatted Text\"");
resFormattedStrTxtView.setText(formattedStr);
// Accessing Resources for Formatted String contents from the resources folder - end

// Accessing Resources for Formatted String contents from the resources folder - start
resStyledStrTxtView=(TextView)findViewById(R.id.resStyledStrTxtId);
/* without styling
String styleStr=getString(R.string.resStyledStrTxt);
resStyledStrTxtView.setText(styleStr);
*/
// with styling
resStyledStrTxtView.setText(R.string.resStyledStrTxt);
// Accessing Resources for Formatted String contents from the resources folder - end
// Working with resources - End
}
}
values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ResourceDemo</string>
<string name="resPlainStrTxt">Plain String Resources - This is referenced from res/values/string.xml</string>
<string name="resFormattedStrTxt">Formatted String Resources - This is referenced from res/values/string.xml and is formatted with %1$s</string>
<string name="resStyledStrTxt">Styled String Resources - This is referenced from res/values/string.xml and is styled with \n<b>bold</b>, \n<u>underline</u> and \n<i>italics</i></string>
</resources>

1 comment:

  1. Good introduction. Have you done any performance analysis on the i/o operations side on android. Just interested to know what kind of response times it gives on a typical hardware.

    ReplyDelete