qwerty_
qwerty_

Reputation: 147

Printing out string of color [Android]

I do random generation of colors in my app, and I have integer array of colors from XML:

<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>
<item name="orange" type="color">#FFFFBB33</item>
<item name="red" type="color">#FFFF4444</item>
<item name="darkblue" type="color">#FF0099CC</item>
<item name="darkpurple" type="color">#FF9933CC</item>
<item name="darkgreen" type="color">#FF669900</item>
<item name="darkorange" type="color">#FFFF8800</item>
<item name="darkred" type="color">#FFCC0000</item>

<integer-array name="androidcolors">
    <item>@color/blue</item>
    <item>@color/purple</item>
    <item>@color/green</item>
    <item>@color/orange</item>
    <item>@color/red</item>
    <item>@color/darkblue</item>
    <item>@color/darkpurple</item>
    <item>@color/darkgreen</item>
    <item>@color/darkorange</item>
    <item>@color/darkred</item>
</integer-array>

and code is:

int[] androidColors = getResources().getIntArray(R.array.androidcolors);
    int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
    layout.setBackgroundColor(randomAndroidColor);
    //layout.addView(textView);
    TextView textView1 = new TextView(this);
    textView1.setTextSize(20);
    String message1 = new String(...);
    textView1.setText(message1);
    layout.addView(textView1);

What have I to put in the String constructor (three dots in it), so in the message1 object will be set to the name of the color, which was generated to integer variable randomAndroidColor??

Upvotes: 1

Views: 117

Answers (1)

Kasun Dissanayake
Kasun Dissanayake

Reputation: 506

The way I see the problem you will have to define a name attribute in items of the int array and get attributes using a XML parser. But I don't think you want to get that much trouble just to get pre-defined color names.

You can simply create another string array and get the names

<integer-array name="androidcolors">
  <item>@color/blue</item>
  <item>@color/purple</item>
  <item>@color/green</item>
  <item>@color/orange</item>
  <item>@color/red</item>
  <item>@color/darkblue</item>
  <item>@color/darkpurple</item>
  <item>@color/darkgreen</item>
  <item>@color/darkorange</item>
  <item>@color/darkred</item>
</integer-array> 

<string-array name="colornames">
  <item>blue</item>
  <item>purple</item>
  <item>green</item>
  <item>orange</item>
  <item>red</item>
  <item>darkblue</item>
  <item>darkpurple</item>
  <item>darkgreen</item>
  <item>darkorange</item>
  <item>darkred</item>
</string-array>

and now you can access to the color names withing the code

int[] androidColors = getResources().getIntArray(R.array.androidcolors);
String [] androidColorsNames = getResources().getStringArray(R.array.colornames);

int randomNumber=new Random().nextInt(androidColors.length);

int randomAndroidColor = androidColors[randomNumber];
layout.setBackgroundColor(randomAndroidColor);
//layout.addView(textView);
TextView textView1 = new TextView(this);
textView1.setTextSize(20);
String message1 = androidColorsNames[randomNumber] ;
textView1.setText(message1);
layout.addView(textView1);

If you want to you use an XML parser anyway, please refer this answer.

Upvotes: 1

Related Questions