Take the name of an item within an array in strings.xml

Asked

Viewed 232 times

0

I have the following array:

<array name="Codes">
        <item name="Vermelho">e74c3c</item>
        <item name="Azul">3498db</item>
        <item name="Rosa">FC14E5</item>
        <item name="Roxo">8e44ad</item>
        <item name="Amarelo">f1c40f</item>
        <item name="Laranja">d35400</item>
        <item name="Verde">2ecc71</item>
        <item name="Cinza">95a5a6</item>
    </array>

and I have a variable that holds the color code

Collections.shuffle(Arrays.asList(codes));
this.colorCode = "#" + codes[0];

how could I catch the name="" ?

  • Where this xml is saved?

  • strings.xml, android default

  • String[] some_array = getResources().getStringArray(R.array.your_string_array) didn’t work out?

  • This would take the color code, which is what I already have, I would like to take the name of the array item, in case, Red

  • Oh yes, you will need an xml parser, fortunately android too owns it. See the link as a reference until someone answers you.

  • Okay, I’ll read it, thanks

Show 1 more comment

1 answer

1


Hello, A solution to your problem would be to create another array that has color names in its values. As in the Example

<array name="Codes">
        <item name="Vermelho">e74c3c</item>
        <item name="Azul">3498db</item>
        <item name="Rosa">FC14E5</item>
        <item name="Roxo">8e44ad</item>
        <item name="Amarelo">f1c40f</item>
        <item name="Laranja">d35400</item>
        <item name="Verde">2ecc71</item>
        <item name="Cinza">95a5a6</item>
    </array>
<array name="Colors">
            <item name="Vermelho">Vermelho</item>
            <item name="Azul">Azul</item>
            <item name="Rosa">Rosa</item>
            <item name="Roxo">Roxo</item>
            <item name="Amarelo">Amarelo</item>
            <item name="Laranja">Laranja</item>
            <item name="Verde">Verde"</item>
            <item name="Cinza">Cinza</item>
        </array>

This way you can catch the name just by using the same index.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.