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


Hello World, AndroidStringResources!
AndroidStringResources
It\'s a exercise about \"String Resources\"!

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday


man
men




Modify main.xml

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/string_1"
/>
android:id="@+id/numberofman"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>



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 adapter
= new ArrayAdapter(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.

Post a Comment

Previous Post Next Post

Contact Form