2

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?

Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
Noby
  • 6,322
  • 9
  • 38
  • 63

3 Answers3

5
public String getString(String abc){ // Ex. abc = "address1"

   int resID = getResources().getIdentifier(abc, "string",  getPackageName()); 

   return context.getResources().getString(resID);
}
Cuarcuiu
  • 437
  • 1
  • 8
  • 19
Pratik
  • 30,557
  • 17
  • 84
  • 156
2
 String abc="StringId";
 int resID = getResources().getIdentifier(abc, "string",  getPackageName()); 
weakwire
  • 9,249
  • 8
  • 52
  • 78
0

try This:

Resources res = this.getResources(); 
 int resID = res.getIdentifier(imagename, "drawable", this.getPackageName());
Ahmad Arslan
  • 4,998
  • 8
  • 40
  • 60