112

How do you access the values in the res/values/string.xml resource file from the Android Activity class?

Trevor.Screws
  • 541
  • 6
  • 18
Ravikiran
  • 2,413
  • 5
  • 25
  • 30

5 Answers5

152

Well you can get String using,

getString(R.string.app_name);

And, you can get string-array using

String arr[] = getResources().getStringArray(R.array.planet);
for (int i = 0; i < arr.length; i++) {
        Toast.makeText(getBaseContext(),arr[i], Toast.LENGTH_LONG).show();  
}
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • MercuryVenus Earth for this code while i use getString(R.string.planets); – Ravikiran Aug 27 '11 at 12:53
  • I checked your answer but I got ForceClose error. Thanks for helping – Ravikiran Aug 27 '11 at 14:31
  • THIS IS LOGCAT 08-27 20:16:04.844: ERROR/AndroidRuntime(339): FATAL EXCEPTION: main 08-27 20:16:04.844: ERROR/AndroidRuntime(339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.string/com.string.string}: android.content.res.Resources$NotFoundException: String resource ID #0x7f050000 – Ravikiran Aug 27 '11 at 15:02
  • THIS IS LOGCAT 08-27 20:16:04.844: ERROR/AndroidRuntime(339): FATAL EXCEPTION: main 08-27 20:16:04.844: ERROR/AndroidRuntime(339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.string/com.string.string}: android.content.res.Resources$NotFoundException: String resource ID #0x7f050000 08-27 20:16:04.844: ERROR/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) – Ravikiran Aug 27 '11 at 15:14
  • This is LogCat information.08-27 22:17:20.980: INFO/AndroidRuntime(299): NOTE: attach of thread 'Binder Thread #3' failed.I used your updated code but stringArray is not displaying on Emulator – Ravikiran Aug 27 '11 at 17:19
  • Thanks for your help. I got required output in LogCat but how to get same output on Emulator. – Ravikiran Aug 28 '11 at 02:45
  • Thanks its working fine but i need to display array at a time. – Ravikiran Aug 28 '11 at 05:58
  • It means, as i used above code it displaying as Mercury wait for second then venus wait for second and then Earth.But i need it to be displayed as Mercury Venus Earth simultaniously. – Ravikiran Aug 28 '11 at 06:32
  • See code that I entered and try it. Following code shows my requirement. Thanks you tried to solve my problem. – Ravikiran Sep 04 '11 at 14:22
  • 1
    Note that getString() is not static. If you want to assign a string.xml value to a static property of your app class, do it in you class onCreate(). – KrisWebDev May 02 '14 at 18:15
46

strings.xml:

<string name="some_text">Some Text</string>

Activity:

getString(R.string.some_text);
Michael
  • 53,859
  • 22
  • 133
  • 139
Ollie C
  • 28,313
  • 34
  • 134
  • 217
16

Put this code in res/values/string.xml

<string-array name="planet"> 
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
</string-array>

This code to be placed in res/layout/main.xml and remove default widgets present in main.xml.

<ListView android:id="@+id/planet"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:entries="@array/planet"/>
</LinearLayout>
Ajith Ramesh
  • 195
  • 14
Ravikiran
  • 2,413
  • 5
  • 25
  • 30
15

If getString(R.string.app_name); isn't working for you, you can pass context like this:

context.getString(R.string.app_name);
Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64
2

If you have the context of Activity, go with:

getString(R.string.app_name);

If you don't have the context, try below, you can get the context from activity using Constructor.

mContext.getString(R.string.app_name);
Nick
  • 138,499
  • 22
  • 57
  • 95
gauravsngarg
  • 606
  • 5
  • 11