Monday, 15 November 2010

String Resources

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings:

String
XML resource that provides a single string.
String Array
XML resource that provides an array of strings.
Plurals
XML resource that carries different strings for different pluralizations of the same word or phrase.

All strings are capable of applying some styling markup and formatting arguments. For information about styling and formatting strings, see the section about Formatting and Styling.



Here are some examples of using String Resources-



Modify /res/values/strings.xml to add our String Resources in XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidStringResources!</string>
<string name="app_name">AndroidStringResources</string>
<string name="string_1">It\'s a exercise about \"String Resources\"!</string>
<string-array name="DayOfWeek">
<item>Sunday</item>
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
</string-array>
<plurals name="NumberOfMan">
<item quantity="one">man</item>
<item quantity="other">men</item>
</plurals>
</resources>


Modify 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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/string_1"
/>
<TextView
android:id="@+id/numberofman"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Java code
package com.exercise.AndroidStringResources;

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

public class AndroidStringResources extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner spinnerDayOfWeek = (Spinner)findViewById(R.id.spinner);
String[] dayOfWeek = getResources().getStringArray(R.array.DayOfWeek);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, dayOfWeek);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDayOfWeek.setAdapter(adapter);

TextView textviewNumberOfMan = (TextView)findViewById(R.id.numberofman);
int numberOfMan = 2;
String stringNumberOfMan
= getResources().getQuantityString(R.plurals.NumberOfMan, numberOfMan);
textviewNumberOfMan.setText("It's " + String.valueOf(numberOfMan) + " "
+ stringNumberOfMan + " here.");
}
}


Download the files.

No comments:

Post a Comment