Home > Net >  Get name/title of a string-array in strings.xml
Get name/title of a string-array in strings.xml

Time:08-07

I've got a string-array

<string-array name="color">
    <item>red</item>
    <item>green</item>
    <item>purple</item>
</string-array>
  1. How could I get it's name with Java? (I want to get a string with "color" inside)

  2. How to I extract it's name to a string? To do smth like this:

     <string-array name="@string/color">
         <item>red</item>
         <item>green</item>
         <item>purple</item>
     </string-array>
    

<string name="color">color</string>

  1. Is it redudant to extract all items of a string-array to strings? like this:

     <string-array name="color">
     <item>@string/red</item>
     <item>@string/green</item>
     <item>@string/purple</item>
    

<string name="red">red</string> <string name="green">green</string> <string name="purple">purple</string>

CodePudding user response:

  1. From the documentation:

    Resources res = getResources(); String[] colors = res.getStringArray(R.array.color);

  2. As far as I know, you can not do this.

  3. It depends on how you want do use the array. For example, if you have more than one language in your app, it makes sense to give the item a value that is a reference to another string resource .

CodePudding user response:

  1. getResources().getResourceEntryName(R.array.variant);
  1. I can't translate strings without extracting them like so.
  • Related