5

I created an application that uses the TTS engine to send feedback to the user. With the aim to improve the performance, I used the synthesizeToFile and addSpeech methods, but strings of text to be synthesized are inside the strings.xml file, so I have to invoke these methods for each string that is spoken by the TTS engine.

Since the TTS engine uses only strings whose name begins with tts_, is it possible to easily iterate over all strings that begin with tts_ within the strings.xml file?

5 Answers 5

12

You can get all the strings in strings.xml via reflection, and filter out only the ones you need, like so:

for (Field field : R.string.class.getDeclaredFields())
{
  if (Modifier.isStatic(field.getModifiers()) && !Modifier.isPrivate(field.getModifiers()) && field.getType().equals(int.class))
  {
    try
    {
      if (field.getName().startsWith("tts_"))
      {
        int id = field.getInt(null);
        // do something here...
      }
    } catch (IllegalArgumentException e)
    {
      // ignore
    } catch (IllegalAccessException e)
    {
      // ignore
    }
  }
}
7
  • 2
    I haven't tried this myself, but provided that it works, THIS is the answer to the question, not the accepted one, which would only work in the ridiculous case where the names tts_1, tts_2, etc were acceptable.
    – matteo
    Oct 5, 2014 at 18:42
  • Could you please specify which Field class is this? Is it java.lang.reflect.Field?
    – matteo
    Oct 5, 2014 at 18:44
  • 1
    Yes, it's java.lang.reflect.Field. It should be easy to find, since it's the return type of class.getDeclaredFields() Oct 6, 2014 at 11:10
  • Is it possible to get the strings of all modules (each R file exists in a different package) ? And, how do I get it for each language? Mar 15, 2017 at 9:54
  • @androiddeveloper Yes, but you must know ahead of time (i.e. hard-code) all the different R classes in each package (e.g. for (Field field : com.example.foo.R.string.class.getDeclaredFields())). There are ways around that too, but they are substantially more complicated than this. Mar 15, 2017 at 18:28
8

You can give them all (while defining) the resource name as "prefix"+(1..n). And in the code use,

int resid=<constant>;
for(i=1;resid!=0;i++){
        resid = this.getResources().getIdentifier("prefix"+i, "strings", this.getPackageName());
}
2

You could put these TTS strings into a TypedArray.

0

you can use this code:

String[] strings = getResources().getAssets().list("string");
for (int i = 0; i < strings.length; i++) {
                Log.d("aaa ", strings[i]);
            }

to iterate through other resources like fonts,... just replace string with folder name.

-1

In all my projects, i just observed that the value of strings in R.java starts with 0x7f050000 and it counts upwards, like 0x7f050001, 0x7f050002, 0x7f050003,....

You could just ++ them :D

Hope it helps :)

1
  • 1
    You cannot assume that it starts from 0x7f050000 if your code hase arrays.xml attr.xml than sometimes it varies.. you are correct that it gives incremental ids but lowest id you can not assume. Dec 22, 2012 at 7:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.