Search code examples
androidxmlandroid-resources

Android localization strings from xml


I have xml folder in my res folder with file structured like this:

    <questions>
        <question
            number="1"
            text="my string" 
            numberTwo="1" />
         ...
    </questions/

So is that possible not to hardcode strings here and use @string/... resources because I really need localization here?


Solution

  • You can have

    text="mystring" in your xml file

    and <string name"mystring">lalala</string> in your strings.xml (possibly localized in other files such as in a folder values-de.xml)

    Then in your code you can do:

    int stringId = getResources().getIdentifier("mystring", "string");
    String string = getString(stringId); // that will be "lalala"
    

    The other way is to have your xml file entirely localized in the folder xml-de but this incurs some text duplication.