How to get the resource name of a string by its value

I want to get the name or identifier of a string resource by passing its value.
Example:

<string name="stringName">stringValue</string> 

I want to pass stringValue and get stringName or ID (id value)

Based on the ID, I will do some calculation to get another identifier of another String resource

+4
source share
2 answers

I do not know if this is possible, and I think that identifiers may change without warning if the class R is newly created. You could, of course, try the magic of reflection in this class, but I would recommend against it.

0
source

I also have this problem, and I can think of two possible workarounds:

  • load all the rows and their names into a table and see Table.
  • Or, run my complete list of names, getting a string resource for each, and comparing it with my famous string resource.

I am implementing the second one, since my list of string resources is not very large, and I do not need to do this operation very often. Once the name is known, you can get the resource identifier through:

 //Get resource id from name var resourceId = (int) typeof (MyApp_droid.Resource.String).GetField(MyStringName).GetValue(null); 

(C # code because I work in Xamarin).

0
source

All Articles