Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Organizing Strings and String Arrays in res/values

I am working on a project that includes a lot of strings and string arrays. I would like to put them into created folders inside res/values, but I get errors when I try to do this. Either getRecources() does not recognize the new folder or the xml attributes cannot link together. I know this is a noob-ish question, but thanks for the help!

like image 334
Brendan Avatar asked Mar 22 '26 03:03

Brendan


1 Answers

Unfortunately, you can't create any subfolders in your values folder. But you have two instruments to control the hierarchy.

String arrays are declared in the following way:

<string-array name="arr_name">
    <item>Text</item>
    <item>Another text</item>
</string-array>

You can access them through R.array.arr_name.

Prefixes are kind of obvious, but since you mentioned that you are a novice, it's worth mentioning. I usually prefix all of my strings depending on how they are used. For example, btn_ for the text used on buttons, dialog_ for strings used in dialogs and so on. This way autocomplete in the IDE also works much better too.

Also you can split your declarations into different files, but this doesn't have any impact at all on the way you access them, so I don't know if this can help you.

like image 128
Malcolm Avatar answered Mar 24 '26 15:03

Malcolm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!