A global string, or array of strings are declared in an external xml file in the resource folder:<project>/res/values/strings.xml
Declaring a global string
<? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >Hello World! < string name = "app_name" >MyTest </ resources > |
Using a global string while creating a TextView
XML example:
< TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> |
Code example:
((TextView)findViewById(R.id.MyTextView)).setText(R.string.hello); |
Getting a global string
String hello = context.getString(R.string.hello); |
Global String Array
Declaration in a Global String Array
<? xml version = "1.0" encoding = "utf-8" ?> < esources > < string-array name = "planets_array" > < item >Mercury</ item > < item >Venus</ item > < item >Earth</ item > < item >Mars</ item > </ string-array > </ resources > |
Code usage
String[] planets = context.getResources().getStringArray(R.array.planets_array); |