Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get list of the strings present in strings.xml file in Android

Tags:

android

I want the list of strings present in the strings.xml file.

Does anyone knows how to get it??? One thing I found is it assigns the ids in sequential order inside R.java but how to get the starting id is not clear.

For Example I have 100 Strings in my strings.xml like below and I want to read in at a time not like giving getResources().getString(int id) for 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>
like image 417
Brijesh Masrani Avatar asked Dec 22 '12 07:12

Brijesh Masrani


People also ask

How do you get string resources?

android:text="@string/hello" /> String string = getString (R. string. hello); You can use either getString(int) or getText(int) to retrieve a string.

In which folder can you find the string resource file strings xml Android?

You can find strings. xml file inside res folder under values as shown below.

How do you access string in Kotlin?

String Element – length – 1). Using index: Returns the character at specified index. Using get function: Returns the character at specified index passed as argument to get function. Iterating over the String: Using loops to access the characters in the String.


1 Answers

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 codes of strings with its values in "str" variable.

like image 184
Miki Avatar answered Oct 13 '22 07:10

Miki