Get list of strings present in strings.xml file in Android

I need a list of strings present in strings.xml.

Does anyone know how to get it ??? One thing I discovered is the assignment of identifiers in sequential order inside R.java, but how to get the initial identifier is not clear.

For example, I have 100 lines in my strings.xml file, as shown below, and I want to read at that time, not as providing getResources (). getString (int id) for the individual.

<string name="app_label">Calendar</string> <string name="what_label">What</string> <string name="where_label">Where</string> <string name="when_label">When</string> <string name="timezone_label">Time zone</string> <string name="attendees_label">Guests</string> <string name="today">Today</string> <string name="tomorrow">Tomorrow</string> 
+8
android
source share
4 answers

If you want to access all the lines from the strings.xml file, you can use reflection in the R.string class. An example can be found in this answer , you just need to replace drawables with strings.

+5
source share

You can declare your lines in res \ values โ€‹โ€‹\ strings.xml as follows.

  <string-array name="vehiclescategory_array"> <item>Cars</item> <item>Bikes</item> <item>RVs</item> <item>Trucks</item> <item>Other Vehicles</item> </string-array> 

In your activity class, you can access them as shown below.

 String[] categories; categories=getResources().getStringArray(R.array.vehiclescategory_array); 

In the list above, any sequence you declare is the same as it is assigned to an array in your activity. Suppose cars are assigned to categories [0]. Hope this helps.

+10
source share
 Field[] fields = R.string.class.getDeclaredFields(); // or Field[] fields = R.string.class.getFields(); String str = ""; for (int i =0; i < fields.length; i++) { int resId = getResources().getIdentifier(fields[i].getName(), "string", getPackageName()); str += fields[i].getName() + " = "; if (resId != 0) { str += getResources().getString(resId); } str += "\n"; } 

You will get all row codes with values โ€‹โ€‹in the str variable.

+5
source share

You can declare an integer array with a record for each row. I did this for an array of colors once, so I think it works for strings too.

RES / value / arrays.xml

 <integer-array name="app_strings"> <item>@string/app_label</item> <item>@string/what_label</item> <item>@string/where_label</item> <item>@string/when_label</item> <item>@string/timezone_label</item> <item>@string/attendees_label</item> <item>@string/today</item> <item>@string/tomorrow</item> </integer-array> 

Then in your code you will iterate over the array and use each value as an argument to getString ().

 int[] stringIds = getResources().getIntArray(R.array.app_strings); String[] strings = new String[stringIds.length]; for (int i = 0; i < stringIds.length; i++) { strings[i] = getString(stringIds[i]); } 

The problem is that you need to manually update the arrays.xml file whenever you change your string resources, so it is definitely not perfect.

+2
source share

All Articles