0

I made an array of items with colors in the colors.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <array name="rectangle_colors">
        <item>
            <color>#77aa3535</color>
        </item>
        <item>
            <color>#44cc1818</color>
        </item>
        <item>
            <color>#ff1068</color>
        </item>
        <item>
            <color>#6090cc</color>
        </item>
        <item>
            <color>#6040aa</color>
        </item>
    </array>
</resources>

I want to access these colors both in Java and in other XML files. I found plenty of help on how to access them in Java, but nothing for accessing them in XML beyond the documentation here. I can't figure out how to use the pattern they provide:

@[package:]array.array_name

I tried this:

android:background="@[com.example.jason.miniproject].array.rectangle_colors[0]"

but it doesn't work.

Is there any way for me to browse all of my XML resource values in Android Studio other than the auto-complete feature that pops up when I start typing in the right spot?

Also, am I going about this wrong way? I have a bunch of views and I want those views to start off with different background colors. I'd like to get those colors from an XML file. Then I want to change those colors in Java as the user interacts with the app. The new colors will be variations of the initial colors, so I'll want to iterate over the initial colors in Java. Is my approach to storing those colors a good idea?

Jason
  • 2,725
  • 2
  • 14
  • 22

2 Answers2

0

Accessing this array in XML it doesn't work. You can store them as you are doing in colors.xml but you can access the colors dynamically, for example, in onCreate() method.

In this example I accessing the first color of Array and set the LinearLayout background with this color:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //
            LinearLayout layout = (LinearLayout) findViewById(R.id.teste);
            Resources resources = getResources();
            int color[] = resources.getIntArray(R.array.rectangle_colors);
            layout.setBackgroundColor(resources.getColor(color[0]));
          }

I read the documentation you posted, but it doesn't tell how to access the desirable item from an array.

In fact it is the same:

@[package]:array/array_name

With

@array/array_name

If your package you are referring is your own

tdmsoares
  • 533
  • 7
  • 24
  • Like I said, I've already found plenty of information on accessing XML arrays in Java. I want to access them in XML. The documentation I linked to indicates that it's possible, but I can't figure out how to use the pattern they provided. I'm starting to think that the pattern is only for accessing the entire array and not for accessing individual elements of the array. – Jason Dec 26 '15 at 04:18
  • As you said, you can only refer to a whole array, not a just item in XML. The problem is the item of an array is not named neither has an Id, so you can only access one item in your code – tdmsoares Dec 26 '15 at 04:40
0

possible workaround

<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="rectangle_colors1">#77aa3535</color>
    <color name="rectangle_colors2">#44cc1818</color>
    <color name="rectangle_colors3">#ff1068</color>
    ....
    <array name="rectangle_colors">
        <item>
            <color>@color/rectangle_colors1</color>
        </item>
        <item>
            <color@color/rectangle_colors2</color>
        </item>
        <item>
            <color>@color/rectangle_colors3</color>
        </item>
       .....
    </array>
</resources>

For more info refer Referencing a string in a string array resource with xml

Community
  • 1
  • 1
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43