user7732300
user7732300

Reputation:

Access strings.xml during runtime to parse

I want to access strings.xml and arrays.xml at runtime ,to parse it and read my custom provided attributes.

Is this possible? If yes, how can i access it? The thing is that I have a questionnaire and I wanted to give each question, that is defined in strings.xml a unique key, so that each question can be identified. This key I've added as a attribute.

<string key="keyForQuestion" name="question1">This is a question?</string>

Maybe there is also a way to access the name attribute? Without parsing it on my own.

Upvotes: 0

Views: 44

Answers (1)

user5366495
user5366495

Reputation:

You can acsess any xml resource by calling:

getResources(). if you're in a Activity or getActivity().getResources(). if you want to get a resource from a Fragment

Exmaple from a Activity:

  //gives us String
 String name =  getResources().getString(R.string.app_name);

 //gives us array of String
 String[] names = getResources().getStringArray(R.string.names);

 //get a specific string from its index
 String myName = names[i]

 //get a specific key from the name
   for(int i =0; i<names.lenght(); i++{
     if(names[i].equels("hello"){
         // Key is > i
       }
    }

Upvotes: 1

Related Questions