Wednesday, 20 July 2011

Using resources of string-array

We can define the content of String array in XML, then load the array with getResources().getStringArray(R.array.xxx).

Create a file /res/values/array.xml to define the array
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<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>
</resources>


package com.exercise.AndroidStringArray;

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

public class AndroidStringArrayActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView)findViewById(R.id.text);

String[] myStringArray = getResources().getStringArray(R.array.DayOfWeek);
String textOut = "";

for(int i = 0; i < myStringArray.length; i++){
textOut += myStringArray[i] + "\n";
}

text.setText(textOut);
}
}



<?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:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>



Using resources of string-array

No comments:

Post a Comment