Android Resources edit

Import array of objects defined in resources

There are cases, where custom objects need to be created and defined in the resources of the application. Such objects can be composed of Java simple types, for example Integer, Float, String.

Here is the example of how to import an object defined in application resources. The object Category constains 3 properties of category:

This POJO has it’s equivalent in categories.xml file, where each of array has the same properties defined for each category.

  1. Create a model for you object:
public class Category {
    private Type id;
    private @ColorRes int color;
    private @StringRes String name;

    public Category getId() {
        return id;

    public void setId(Category id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public int getColor() {
        return color;

    public void setColor(int color) {
        this.color = color;

    public enum Type{
  1. Create the file in res/values folder:


  1. Compose each model consisting of resources:
<array name="no_action">
<array name="to_accept">
<array name="opened">
<array name="to_verify">
<array name="to_close">
  1. Define an array in resources file:
<array name="categories">
  1. Create a function to import them:
public List<Category> getCategories(@NonNull Context context) {
final int DEFAULT_VALUE = 0;
final int ID_INDEX = 0;
final int COLOR_INDEX = 1;
final int LABEL_INDEX = 2;

if (context == null) {
    return Collections.emptyList();
// Get the array of objects from the `tasks_categories` array
TypedArray statuses = context.getResources().obtainTypedArray(R.array.categories);
if (statuses == null) {
    return Collections.emptyList();
List<Category> categoryList = new ArrayList<>();
for (int i = 0; i < statuses.length(); i++) {
    int statusId = statuses.getResourceId(i, DEFAULT_VALUE);
    // Get the properties of one object
    TypedArray rawStatus = context.getResources().obtainTypedArray(statusId);

    Category category = new Category();

    int id = rawStatus.getInteger(ID_INDEX, DEFAULT_VALUE);
    Category.Type categoryId;
    //The ID's should maintain the order with `Category.Type`
    switch (id) { 
        case 0:
            categoryId = Category.Type.REGISTRATION;
        case 1:
            categoryId = Category.Type.TO_ACCEPT;
        case 2:
            categoryId = Category.Type.TO_COMPLETE;
        case 3:
            categoryId = Category.Type.TO_VERIFY;
        case 4:
            categoryId  = Category.Type.CLOSED;
            categoryId = Category.Type.REGISTRATION;

    category.setColor(rawStatus.getResourceId(COLOR_INDEX, DEFAULT_VALUE));

    int labelId = rawStatus.getResourceId(LABEL_INDEX, DEFAULT_VALUE);
    category.setName(getString(context.getResources(), labelId));

return taskCategoryList;

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents
9 Resources
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close