Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get color list from resource

I have an array with names and color codes. I want to color the cell in gridview with colors taken from array. I created this code but I get NPE, this is the error:

Logcat Output:

Caused by: java.lang.NullPointerException
10-06 11:03:30.513 25902-25902/? E/AndroidRuntime:
at android.graphics.Color.parseColor(Color.java:211)

Color XML File:

 <array name="colors">
<item name="White">#FFFFFF</item>
    <item name="Ivory">#FFFFF0</item>
    <item name="LightYellow">#FFFFE0</item>
    <item name="Yellow">#FFFF00</item>
    <item name="Snow">#FFFAFA</item>
    <item name="FloralWhite">#FFFAF0</item>
 </array>

SourceCode:

public class ColorPickerAdapter extends BaseAdapter {

      private Context context;
      private List<Integer> colorList = new ArrayList<Integer>();

      public ColorPickerAdapter(Context context) {
           this.context = context;
           String colors[] = context.getResources().getStringArray(R.array.colors);

           colorList = new ArrayList<Integer>();

            // add the color array to the list
           for (int i = 0; i < colors.length; i++) {
                colorList.add(Color.parseColor(colors[i]));
            }
           [..]
like image 844
user2847219 Avatar asked Nov 10 '15 10:11

user2847219


People also ask

How do you get color from resources?

Use ResourcesCompat. getColor(App. getRes(), R. color.

How can I get state color list?

Color state lists belong in res/color , not res/drawable . This is now deprecated, use ContextCompat. getColorStateList() .

What is ColorStateList in Android?

A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied.

What is a selector Android?

A selector may be created by invoking the open method of this class, which will use the system's default selector provider to create a new selector. A selector may also be created by invoking the openSelector method of a custom selector provider. A selector remains open until it is closed via its close method.


1 Answers

Your resources seems wrong. What you should do

METHOD 1

In colors.xml

<color name="Ivory">#FFFFF0</color>
<color name="LightYellow">#FFFFE0</color>
<color name="Yellow">#FFFF00</color>
<color name="Snow">#FFFAFA</color>
<color name="FloralWhite">#FFFAF0</color>

In arrays.xml :

<array name="colors">
    <item>@color/Ivory</item>
    <item>@color/LightYellow</item>
    <item>@color/Yellow</item>
    <item>@color/Snow</item>
</array>

Then access using

int[] colors = context.getResources().getIntArray(R.array.colors);

for (int i = 0; i < tileColumns; i++) {
    colorList.add(colors[i]);
}

METHOD 2

Do not specify the name of the differents colors

<resources>
    <string-array name="colors">        
        <item>#FFFFF0</item>
        <item>#FFFFE0</item>  
        <item>#FFFF00</item>
        <item>#FFFAFA</item>
        <item>#FFFAF0</item>
    </string-array>
</resources>

and use it using

String colors[] = context.getResources().getStringArray(R.array.colors);
colorList = new ArrayList<Integer>();

// add the color array to the list
for (int i = 0; i < colors.length; i++) {
    colorList.add(Color.parseColor(colors[i]));
}
like image 119
ThomasThiebaud Avatar answered Sep 24 '22 20:09

ThomasThiebaud