Otuyh
Otuyh

Reputation: 2484

Get values from xml dynamically

i have this question about get string values without use an string-array on xml, i just don't know if its possible.

My file.xml can have both this

<resources>
   <string name="test1"></string>
   <string name="test2"></string>
</resources>

and this:

<resources>
   <string name="test1"></string>
</resources>

Is there any way to get the values programatically, without an array.

The problem is, i can't do this:

R.string.test1; 
R.string.test2;

Because not always i have the "test2" string. Is there any way to get all the values dinamically?

Thanks.

Upvotes: 0

Views: 91

Answers (2)

Zohaib Brohi
Zohaib Brohi

Reputation: 574

Try this

String test1 = getResources().getString(R.string.test1);
String test2 = getResources().getString(R.string.test2);

more info

Upvotes: -1

Herr K
Herr K

Reputation: 1791

You could receive the string with its name (instead of its id):

   String packageName = getPackageName();
   int resId = getResources().getIdentifier("test2", "string", packageName);
   String test2 =getString(resId);

Upvotes: 2

Related Questions