Reputation: 6602
I have many string elements in my res/values/strings.xml
So, I want one method getString(String abc)
for retrieving the strings from strings.xml
:
public String getString(String abc){ // abc = address1
String result;
result = context.getResources().getString(R.strings.+abc);
}
How to access the string elements in this method based on a String in argument?
Upvotes: 2
Views: 1408
Reputation: 4528
try This:
Resources res = this.getResources();
int resID = res.getIdentifier(imagename, "drawable", this.getPackageName());
Upvotes: 0
Reputation: 30855
public String getString(String abc){ // Ex. abc = "address1"
int resID = getResources().getIdentifier(abc, "string", getPackageName());
return context.getResources().getString(resID);
}
Upvotes: 5
Reputation: 9300
String abc="StringId";
int resID = getResources().getIdentifier(abc, "string", getPackageName());
Upvotes: 2