Android getting a list of colors from resource
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);
}
Why not simply create two arrays in in your strings.xml
or arrays.xml
. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="items">
<item>white</item>
<item>yellow</item>
<item>fuchsia</item>
<item>red</item>
</string-array>
<string-array name="values">
<item>#FFFFFF</item>
<item>#FFFF00</item>
<item>#FF00FF</item>
<item>#FF0000</item>
</string-array>
</resources>
You can follow a simple example on developer.android.com here: http://developer.android.com/guide/topics/ui/controls/spinner.html.
This is another tutorial: http://www.dcpagesapps.com/developer-resources/android/21-android-tutorial-spinners?start=1
That should get you started.
I got stuck doing this for my project. It took long time to make this and i hope it helps some people. I ended up combining an example from here
http://www.ipcas.com/blog/2011/12/android-color-picker/
and a color list provided from here
Web colors in an Android color xml resource file
Just delete colors you dont need and make whatever you like. (Alot faster than making your own)
This is my Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AppName</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_visitor">Visitor</string>
<!-- Translation: Hint: White, Blue, Red...-->
<string name="accolor">Hint: White, Blue, Red…</string>
<integer name="colorGridColumnWidth">65</integer>
<color name="AliceBlue">#F0F8FF</color>
<color name="AntiqueWhite">#FAEBD7</color>
<color name="aqua">#00FFFF</color>
<color name="Aqua">#00FFFF</color>
<color name="Aquamarine">#7FFFD4</color>
<color name="Azure">#F0FFFF</color>
<color name="Beige">#F5F5DC</color>
<color name="Bisque">#FFE4C4</color>
<color name="black">#000000</color>
<color name="Black">#000000</color>
<color name="BlanchedAlmond">#FFEBCD</color>
<color name="Blue">#0000FF</color>
<color name="BlueViolet">#8A2BE2</color>
<color name="Brown">#A52A2A</color>
<color name="BurlyWood">#DEB887</color>
<color name="CadetBlue">#5F9EA0</color>
<color name="Chartreuse">#7FFF00</color>
<color name="Chocolate">#D2691E</color>
<color name="Coral">#FF7F50</color>
<color name="CornflowerBlue">#6495ED</color>
<color name="Cornsilk">#FFF8DC</color>
<color name="Crimson">#DC143C</color>
<color name="Cyan">#00FFFF</color>
<color name="DarkBlue">#00008B</color>
<color name="DarkCyan">#008B8B</color>
<color name="DarkGoldenrod">#B8860B</color>
<color name="DarkGray">#A9A9A9</color>
<color name="DarkGreen">#006400</color>
<color name="DarkKhaki">#BDB76B</color>
<color name="DarkMagenta">#8B008B</color>
<color name="DarkOliveGreen">#556B2F</color>
<color name="DarkOrange">#FF8C00</color>
<color name="DarkOrchid">#9932CC</color>
<color name="DarkRed">#8B0000</color>
<color name="DarkSalmon">#E9967A</color>
<color name="DarkSeaGreen">#8FBC8F</color>
<color name="DarkSlateBlue">#483D8B</color>
<color name="DarkSlateGray">#2F4F4F</color>
<color name="DarkTurquoise">#00CED1</color>
<color name="DarkViolet">#9400D3</color>
<color name="DeepPink">#FF1493</color>
<color name="DeepSkyBlue">#00BFFF</color>
<color name="DimGray">#696969</color>
<color name="DodgerBlue">#1E90FF</color>
<color name="dullwhite">#FFDDDDDD</color>
<color name="FireBrick">#B22222</color>
<color name="FloralWhite">#FFFAF0</color>
<color name="ForestGreen">#228B22</color>
<color name="fuchsia">#FF00FF</color>
<color name="Fuchsia">#FF00FF</color>
<color name="Gainsboro">#DCDCDC</color>
<color name="GhostWhite">#F8F8FF</color>
<color name="Gold">#FFD700</color>
<color name="Goldenrod">#DAA520</color>
<color name="gray">#808080</color>
<color name="Gray">#808080</color>
<color name="green">#008000</color>
<color name="Green">#008000</color>
<color name="GreenYellow">#ADFF2F</color>
<color name="Honeydew">#F0FFF0</color>
<color name="HotPink">#FF69B4</color>
<color name="IndianRed">#CD5C5C</color>
<color name="Indigo">#4B0082</color>
<color name="Ivory">#FFFFF0</color>
<color name="Khaki">#F0E68C</color>
<color name="Lavender">#E6E6FA</color>
<color name="LavenderBlush">#FFF0F5</color>
<color name="LawnGreen">#7CFC00</color>
<color name="LemonChiffon">#FFFACD</color>
<color name="LightBlue">#ADD8E6</color>
<color name="LightCoral">#F08080</color>
<color name="LightCyan">#E0FFFF</color>
<color name="LightGoldenrodYellow">#FAFAD2</color>
<color name="LightGreen">#90EE90</color>
<color name="LightGrey">#D3D3D3</color>
<color name="LightPink">#FFB6C1</color>
<color name="LightSalmon">#FFA07A</color>
<color name="LightSeaGreen">#20B2AA</color>
<color name="LightSkyBlue">#87CEFA</color>
<color name="LightSlateGray">#778899</color>
<color name="LightSteelBlue">#B0C4DE</color>
<color name="LightYellow">#FFFFE0</color>
<color name="lime">#00FF00</color>
<color name="Lime">#00FF00</color>
<color name="LimeGreen">#32CD32</color>
<color name="Linen">#FAF0E6</color>
<color name="Magenta">#FF00FF</color>
<color name="maroon">#800000</color>
<color name="Maroon">#800000</color>
<color name="MediumAquamarine">#66CDAA</color>
<color name="MediumBlue">#0000CD</color>
<color name="MediumOrchid">#BA55D3</color>
<color name="MediumPurple">#9370DB</color>
<color name="MediumSeaGreen">#3CB371</color>
<color name="MediumSlateBlue">#7B68EE</color>
<color name="MediumSpringGreen">#00FA9A</color>
<color name="MediumTurquoise">#48D1CC</color>
<color name="MediumVioletRed">#C71585</color>
<color name="MidnightBlue">#191970</color>
<color name="MintCream">#F5FFFA</color>
<color name="MistyRose">#FFE4E1</color>
<color name="Moccasin">#FFE4B5</color>
<color name="NavajoWhite">#FFDEAD</color>
<color name="navy">#000080</color>
<color name="Navy">#000080</color>
<color name="OldGold">#CFB53B</color>
<color name="OldLace">#FDF5E6</color>
<color name="olive">#808000</color>
<color name="Olive">#808000</color>
<color name="OliveDrab">#6B8E23</color>
<color name="Orange">#FFA500</color>
<color name="OrangeRed">#FF4500</color>
<color name="Orchid">#DA70D6</color>
<color name="PaleGoldenrod">#EEE8AA</color>
<color name="PaleGreen">#98FB98</color>
<color name="PaleTurquoise">#AFEEEE</color>
<color name="PaleVioletRed">#DB7093</color>
<color name="PapayaWhip">#FFEFD5</color>
<color name="PeachPuff">#FFDAB9</color>
<color name="Peru">#CD853F</color>
<color name="Pink">#FFC0CB</color>
<color name="Plum">#DDA0DD</color>
<color name="PowderBlue">#B0E0E6</color>
<color name="purple">#800080</color>
<color name="Purple">#800080</color>
<color name="red">#FF0000</color>
<color name="Red">#FF0000</color>
<color name="RosyBrown">#BC8F8F</color>
<color name="RoyalBlue">#4169E1</color>
<color name="SaddleBrown">#8B4513</color>
<color name="Salmon">#FA8072</color>
<color name="SandyBrown">#F4A460</color>
<color name="SeaGreen">#2E8B57</color>
<color name="Seashell">#FFF5EE</color>
<color name="Sienna">#A0522D</color>
<color name="silver">#C0C0C0</color>
<color name="Silver">#C0C0C0</color>
<color name="SkyBlue">#87CEEB</color>
<color name="SlateBlue">#6A5ACD</color>
<color name="SlateGray">#708090</color>
<color name="Snow">#FFFAFA</color>
<color name="SpringGreen">#00FF7F</color>
<color name="SteelBlue">#4682B4</color>
<color name="Tan">#D2B48C</color>
<color name="teal">#008080</color>
<color name="Teal">#008080</color>
<color name="Thistle">#D8BFD8</color>
<color name="Tomato">#FF6347</color>
<color name="Turquoise">#40E0D0</color>
<color name="Violet">#EE82EE</color>
<color name="Wheat">#F5DEB3</color>
<color name="white">#FFFFFFFF</color>
<color name="White">#FFFFFF</color>
<color name="WhiteSmoke">#F5F5F5</color>
<color name="yellow">#FFFF00</color>
<color name="Yellow">#FFFF00</color>
<color name="YellowGreen">#9ACD32</color>
<array name="colorNameList">
<item>AliceBlue</item>
<item>AntiqueWhite</item>
<item>Aqua</item>
<item>Aquamarine</item>
<item>Azure</item>
<item>Beige</item>
<item>Bisque</item>
<item>Black</item>
<item>BlanchedAlmond</item>
<item>Blue</item>
<item>BlueViolet</item>
<item>Brown</item>
<item>BurlyWood</item>
<item>CadetBlue</item>
<item>Chartreuse</item>
<item>Chocolate</item>
<item>Coral</item>
<item>CornflowerBlue</item>
<item>Cornsilk</item>
<item>Crimson</item>
<item>Cyan</item>
<item>DarkBlue</item>
<item>DarkCyan</item>
<item>DarkGoldenrod</item>
<item>DarkGray</item>
<item>DarkGreen</item>
<item>DarkKhaki</item>
<item>DarkMagenta</item>
<item>DarkOliveGreen</item>
<item>DarkOrange</item>
<item>DarkOrchid</item>
<item>DarkRed</item>
<item>DarkSalmon</item>
<item>DarkSeaGreen</item>
<item>DarkSlateBlue</item>
<item>DarkSlateGray</item>
<item>DarkTurquoise</item>
<item>DarkViolet</item>
<item>DeepPink</item>
<item>DeepSkyBlue</item>
<item>DimGray</item>
<item>DodgerBlue</item>
<item>FireBrick</item>
<item>FloralWhite</item>
<item>ForestGreen</item>
<item>Fuchsia</item>
<item>Gainsboro</item>
<item>GhostWhite</item>
<item>Gold</item>
<item>Goldenrod</item>
<item>Gray</item>
<item>Green</item>
<item>GreenYellow</item>
<item>Honeydew</item>
<item>HotPink</item>
<item>IndianRed</item>
<item>Indigo</item>
<item>Ivory</item>
<item>Khaki</item>
<item>Lavender</item>
<item>LavenderBlush</item>
<item>LawnGreen</item>
<item>LemonChiffon</item>
<item>LightBlue</item>
<item>LightCoral</item>
<item>LightCyan</item>
<item>LightGoldenrodYellow</item>
<item>LightGreen</item>
<item>LightGrey</item>
<item>LightPink</item>
<item>LightSalmon</item>
<item>LightSeaGreen</item>
<item>LightSkyBlue</item>
<item>LightSlateGray</item>
<item>LightSteelBlue</item>
<item>LightYellow</item>
<item>Lime</item>
<item>LimeGreen</item>
<item>Linen</item>
<item>Magenta</item>
<item>Maroon</item>
<item>MediumAquamarine</item>
<item>MediumBlue</item>
<item>MediumOrchid</item>
<item>MediumPurple</item>
<item>MediumSeaGreen</item>
<item>MediumSlateBlue</item>
<item>MediumSpringGreen</item>
<item>MediumTurquoise</item>
<item>MediumVioletRed</item>
<item>MidnightBlue</item>
<item>MintCream</item>
<item>MistyRose</item>
<item>Moccasin</item>
<item>NavajoWhite</item>
<item>Navy</item>
<item>OldGold</item>
<item>OldLace</item>
<item>Olive</item>
<item>OliveDrab</item>
<item>Orange</item>
<item>OrangeRed</item>
<item>Orchid</item>
<item>PaleGoldenrod</item>
<item>PaleGreen</item>
<item>PaleTurquoise</item>
<item>PaleVioletRed</item>
<item>PapayaWhip</item>
<item>PeachPuff</item>
<item>Peru</item>
<item>Pink</item>
<item>Plum</item>
<item>PowderBlue</item>
<item>Purple</item>
<item>Red</item>
<item>RosyBrown</item>
<item>RoyalBlue</item>
<item>SaddleBrown</item>
<item>Salmon</item>
<item>SandyBrown</item>
<item>SeaGreen</item>
<item>Seashell</item>
<item>Sienna</item>
<item>Silver</item>
<item>SkyBlue</item>
<item>SlateBlue</item>
<item>SlateGray</item>
<item>Snow</item>
<item>SpringGreen</item>
<item>SteelBlue</item>
<item>Tan</item>
<item>Teal</item>
<item>Thistle</item>
<item>Tomato</item>
<item>Turquoise</item>
<item>Violet</item>
<item>Wheat</item>
<item>WhiteSmoke</item>
<item>Yellow</item>
<item>YellowGreen</item>
</array>
<array name="colorNumberList">
<item>@color/AliceBlue</item>
<item>@color/AntiqueWhite</item>
<item>@color/Aqua</item>
<item>@color/Aquamarine</item>
<item>@color/Azure</item>
<item>@color/Beige</item>
<item>@color/Bisque</item>
<item>@color/Black</item>
<item>@color/BlanchedAlmond</item>
<item>@color/Blue</item>
<item>@color/BlueViolet</item>
<item>@color/Brown</item>
<item>@color/BurlyWood</item>
<item>@color/CadetBlue</item>
<item>@color/Chartreuse</item>
<item>@color/Chocolate</item>
<item>@color/Coral</item>
<item>@color/CornflowerBlue</item>
<item>@color/Cornsilk</item>
<item>@color/Crimson</item>
<item>@color/Cyan</item>
<item>@color/DarkBlue</item>
<item>@color/DarkCyan</item>
<item>@color/DarkGoldenrod</item>
<item>@color/DarkGray</item>
<item>@color/DarkGreen</item>
<item>@color/DarkKhaki</item>
<item>@color/DarkMagenta</item>
<item>@color/DarkOliveGreen</item>
<item>@color/DarkOrange</item>
<item>@color/DarkOrchid</item>
<item>@color/DarkRed</item>
<item>@color/DarkSalmon</item>
<item>@color/DarkSeaGreen</item>
<item>@color/DarkSlateBlue</item>
<item>@color/DarkSlateGray</item>
<item>@color/DarkTurquoise</item>
<item>@color/DarkViolet</item>
<item>@color/DeepPink</item>
<item>@color/DeepSkyBlue</item>
<item>@color/DimGray</item>
<item>@color/DodgerBlue</item>
<item>@color/FireBrick</item>
<item>@color/FloralWhite</item>
<item>@color/ForestGreen</item>
<item>@color/Fuchsia</item>
<item>@color/Gainsboro</item>
<item>@color/GhostWhite</item>
<item>@color/Gold</item>
<item>@color/Goldenrod</item>
<item>@color/Gray</item>
<item>@color/Green</item>
<item>@color/GreenYellow</item>
<item>@color/Honeydew</item>
<item>@color/HotPink</item>
<item>@color/IndianRed</item>
<item>@color/Indigo</item>
<item>@color/Ivory</item>
<item>@color/Khaki</item>
<item>@color/Lavender</item>
<item>@color/LavenderBlush</item>
<item>@color/LawnGreen</item>
<item>@color/LemonChiffon</item>
<item>@color/LightBlue</item>
<item>@color/LightCoral</item>
<item>@color/LightCyan</item>
<item>@color/LightGoldenrodYellow</item>
<item>@color/LightGreen</item>
<item>@color/LightGrey</item>
<item>@color/LightPink</item>
<item>@color/LightSalmon</item>
<item>@color/LightSeaGreen</item>
<item>@color/LightSkyBlue</item>
<item>@color/LightSlateGray</item>
<item>@color/LightSteelBlue</item>
<item>@color/LightYellow</item>
<item>@color/Lime</item>
<item>@color/LimeGreen</item>
<item>@color/Linen</item>
<item>@color/Magenta</item>
<item>@color/Maroon</item>
<item>@color/MediumAquamarine</item>
<item>@color/MediumBlue</item>
<item>@color/MediumOrchid</item>
<item>@color/MediumPurple</item>
<item>@color/MediumSeaGreen</item>
<item>@color/MediumSlateBlue</item>
<item>@color/MediumSpringGreen</item>
<item>@color/MediumTurquoise</item>
<item>@color/MediumVioletRed</item>
<item>@color/MidnightBlue</item>
<item>@color/MintCream</item>
<item>@color/MistyRose</item>
<item>@color/Moccasin</item>
<item>@color/NavajoWhite</item>
<item>@color/Navy</item>
<item>@color/OldGold</item>
<item>@color/OldLace</item>
<item>@color/Olive</item>
<item>@color/OliveDrab</item>
<item>@color/Orange</item>
<item>@color/OrangeRed</item>
<item>@color/Orchid</item>
<item>@color/PaleGoldenrod</item>
<item>@color/PaleGreen</item>
<item>@color/PaleTurquoise</item>
<item>@color/PaleVioletRed</item>
<item>@color/PapayaWhip</item>
<item>@color/PeachPuff</item>
<item>@color/Peru</item>
<item>@color/Pink</item>
<item>@color/Plum</item>
<item>@color/PowderBlue</item>
<item>@color/Purple</item>
<item>@color/Red</item>
<item>@color/RosyBrown</item>
<item>@color/RoyalBlue</item>
<item>@color/SaddleBrown</item>
<item>@color/Salmon</item>
<item>@color/SandyBrown</item>
<item>@color/SeaGreen</item>
<item>@color/Seashell</item>
<item>@color/Sienna</item>
<item>@color/Silver</item>
<item>@color/SkyBlue</item>
<item>@color/SlateBlue</item>
<item>@color/SlateGray</item>
<item>@color/Snow</item>
<item>@color/SpringGreen</item>
<item>@color/SteelBlue</item>
<item>@color/Tan</item>
<item>@color/Teal</item>
<item>@color/Thistle</item>
<item>@color/Tomato</item>
<item>@color/Turquoise</item>
<item>@color/Violet</item>
<item>@color/Wheat</item>
<item>@color/WhiteSmoke</item>
<item>@color/Yellow</item>
<item>@color/YellowGreen</item>
</array>
</resources>
Next i used the ColorPickerDialog provided by
http://www.ipcas.com/blog/2011/12/android-color-picker/
public class ColorPickerDialog extends Dialog {
public ColorPickerDialog(Context context) {
super(context);
this.setTitle("ColorPickerDialog");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_picker);
GridView gridViewColors = (GridView) findViewById(R.id.gridViewColors);
gridViewColors.setAdapter(new ColorPickerAdapter(getContext()));
// close the dialog on item click
gridViewColors.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ColorPickerDialog.this.dismiss();
}
});
}
}
with this as well.
public class ColorPickerAdapter extends BaseAdapter {
private Context context;
// width of grid column
int colorGridColumnWidth;
//array of color values
int[] colorNumberarray;
// array of color names
String[] colorNameArray;
public ColorPickerAdapter(Context context) {
this.context = context;
// defines the width of each color square
colorGridColumnWidth = context.getResources().getInteger(R.integer.colorGridColumnWidth);
//fill colorNumberarray with values from the colorNumberList Array in strings.xml
colorNumberarray = context.getResources().getIntArray(R.array.colorNumberList);
//fill colorNameArray with values from the colorNameArray Array in strings.xml
colorNameArray = context.getResources().getStringArray(R.array.colorNameList);
//Show results in verbose Log
for (int i = 0; i < colorNumberarray.length; i++) {
Log.v("Test", colorNumberarray[i] + "");
Log.v("Test", colorNameArray[i] + "");
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
// can we reuse a view?
if (convertView == null) {
imageView = new ImageView(context);
// set the width of each color square
imageView.setLayoutParams(new GridView.LayoutParams(colorGridColumnWidth, colorGridColumnWidth));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundColor(colorNumberarray[position]);
imageView.setId(position);
return imageView;
}
public int getCount() {
return colorNumberarray.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
Hopefully this will save people some time.
Next you launch the dialog,
IBcolorpicker.setOnClickListener(new View.OnClickListener() {
Dialog dialog;
@Override
public void onClick(View v) {
// Clicked!
// Make a new dialog.
dialog = new ColorPickerDialog(Visitor.this);
dialog.show();
}
});