Home > development > Android: Get String From Resource By Name

Android: Get String From Resource By Name

November 29th, 2009 Leave a comment Go to comments

Usually the string resources in your Android application are being referred to by a unique ID. But sometimes you will want to recover a string by its key, e. g. when you build the key of the string at the run time of your application (“Battery_Health_2″ or “Battery_Health_3″). You can do that like this:

private String getStringResourceByName(String aString)
{
  String packageName = "com.coliena.myapp";
  int resId = getResources().getIdentifier(aString, "string", packageName);
  return getString(resId);
}

Check this link if you need to recover an image resource by its name.
More information about string resources is available in the Android Developer Documentation.

Tags: ,
  1. Mathias
    November 6th, 2011 at 17:25 | #1

    Thanks a lot for the tip, I’ve been searching for this for a while…

  1. No trackbacks yet.

 
Performance Optimization WordPress Plugins by W3 EDGE