6

I want to get an array of strings reading from arrays.xml file we add in android values/ folder. Could any one kindly give a solution for this. Otherwise I will have to input each these entries in strings.xml and take them to java code using getResources()getString()

Thank you. Tharindu

4 Answers 4

10
getResources().getStringArray(R.array.umc_profile_values);

worked 4 me!

Sign up to request clarification or add additional context in comments.

Comments

6
Resources res = getResources();
String[] name = res.getStringArray(R.array.name_array);

Comments

2

I don't have an IDE in front of me right now, but I think you should be able to do something like R.res.arrays.YOUR_ARRAY_OF_STRINGS.

2 Comments

But it is only an integer I think, I need the content of the array :( thought there should be an easy way other than getting all array elements reading from 'strings.xml'
You can do it by doing the following: String[] arrayContents = getResources.getStringArray(R.array.arrayname);
2
<resources>

    <string name="app_name">App name</string>
    ...

    <string-array name="scale">
        <item>Excellent</item>
        <item>Good</item>
        <item>Fair</item>
        <item>Poor</item>
    </string-array>

</resources>

If you want to get 1 element in array by java

String val1 = getResources().getStringArray(R.array.scale)[0];

Or you can get all

String[] values = getResources().getStringArray(R.array.scale);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.