Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getting a list of colors from resource

I have a list of colours

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="white">#FFFFFF</color>
 <color name="yellow">#FFFF00</color>
 <color name="fuchsia">#FF00FF</color>
 <color name="red">#FF0000</color>
 <color name="silver">#C0C0C0</color>
 <color name="gray">#808080</color>
 <color name="olive">#808000</color>
 <color name="purple">#800080</color>
 <color name="maroon">#800000</color>
 <color name="aqua">#00FFFF</color>
 <color name="lime">#00FF00</color>
 <color name="teal">#008080</color>
 <color name="green">#008000</color>
 <color name="blue">#0000FF</color>
 <color name="navy">#000080</color>
 <color name="black">#000000</color>
</resources>

(I took this list from another question someone asked)

What I want to do is get all the color names. I want the list so I can then populate the lot in a spinner. The pseudo code would be like this.

List ofColours = getListOfColors(R.color);
for(int i = 0 ; i < ofColours.size() ; i ++)
{
  String colour = getResources().getColor(ofColours.get(i));
  addColourToSpinner(colour);
} 

What I want is the list of colours. I hope that explains it Cheers for all the help

like image 910
bubblebath Avatar asked Nov 12 '12 13:11

bubblebath


1 Answers

You can access the list of colors using reflection:

Field[] fields = Class.forName(getPackageName()+".R$color").getDeclaredFields();
for(Field field : fields) {
    String colorName = field.getName();
    int colorId = field.getInt(null);
    int color = getResources().getColor(colorId);
    Log.i("test", colorName + " => " + colorId + " => " + color);
}
like image 72
sdabet Avatar answered Oct 03 '22 06:10

sdabet