Access string.xml resource file from Java Android Code

How to access values ​​in res/values/string.xml res/values/string.xml from Activity class Android Activity class ?

+90
java android string
Aug 27 '11 at 10:23
source share
5 answers

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 needs to be placed in res/layout/main.xml and remove the 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> 
+15
Sep 04 2018-11-11T00:
source share

Well, you can use String,

 getString(R.string.app_name); 

And you can get a 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(); } 
+131
Aug 27 '11 at 10:28
source share

strings.xml:

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

Activity:

 getString(R.string.some_text); 
+40
Aug 27 '11 at 10:24
source share

If getString (R.string.app_name); does not work for you, you can pass this context:

 context.getString(R.string.app_name); 
+14
Jul 31 '13 at 13:04 on
source share

If you have an action context, go with:

 getString(R.string.app_name); 

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

 mContext.getString(R.string.app_name); 
0
Apr 14 '18 at 7:12
source share



All Articles