Skip to content Skip to sidebar Skip to footer

String Array In Res/values/strings.xml

I would like to know how to create an array of strings inside strings.xml, and how to access it in java code. I need this in order to use the setText(). Please help.

Solution 1:

strings.xml

<?xml version="1.0" encoding="utf-8"?><resources><string-arrayname="planets_array"> // name of the string array
    <item>Mercury</item> // items
    <item>Venus</item><item>Earth</item><item>Mars</item></string-array></resources>

In your activity class.

  Resources res = getResources(); 
  String[] planets = res.getStringArray(R.array.planets_array);

                 orString[] planets = getResources().getStringArray(R.array.planets_array);

getResource requires activity context. If its in a non activity class you will need the activity context which can be passed to the non activity class contructor.

Source @

http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

Edit:

newCustomAdapter(ActivityName.this);

Then

classCustomAdapterextendsBaseAdapter
  {
      Context mContext;
      publicCustomAdapter(Context context)
      {
           mContext = context;
      }
   ...// rest of the code
  }

use mContext.getResources()

Solution 2:

You create a String array in strings.xml like such:

<string-arrayname="my_string_array"><item>first string</item><item>second</item></string-array>

And then you can access it with the following:

String[] str = getResources().getStringArray(R.array.my_string_array);

Post a Comment for "String Array In Res/values/strings.xml"