4

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

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

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

Based on ID i will do some calculation to get another ID of another String resource

user1725145
  • 3,993
  • 2
  • 37
  • 58
Kasser
  • 51
  • 1

2 Answers2

0

I don't know if that's possible and I think the IDs can change without warning if the R class gets newly generated. You could of course try some reflection magic on this class, but I would recommend against it.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • That's right kolja.. but i can play on the resource name even if the id is changed – Kasser Jan 04 '13 at 13:42
  • I don't think you can find the resource's id using it's value. – hardartcore Jan 04 '13 at 14:16
  • Actually . it's possible by reading the contents of String.xml as a text file .. But it's not the right way. I'm wondering if there are some functions to do that. – Kasser Jan 04 '13 at 18:03
  • Maybe you would be better off using an XML file or some other means to store your Strings. I still haven't understood what exactly you're trying to do there. – koljaTM Jan 04 '13 at 19:23
0

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

  • load all the strings and their names into a table, and look in the table.
  • Or cycle through my complete list of names, getting the string resource for each one, and comparing it to my known string resource.

I am implementing the 2nd one, as my list of string resources is not very big, and I don't have to do this operation very often. Once the name is known, it's possible to get the Resource Id via:

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

(code is C# because I'm working in Xamarin).

user1725145
  • 3,993
  • 2
  • 37
  • 58