I'm trying to make a program generate a text randomly and let that text be color randomly too but I don't know how. Can be in either Java or Kotlin.
Asked
Active
Viewed 591 times
-11
-
Look for some tutorials – Apurva Kolapkar Dec 22 '17 at 10:02
-
Please do Google this is the simple question, you definitely get the answer. – Fenil Patel Dec 22 '17 at 10:13
-
I want to make like this random number == 1 print("RED") number == 2 blue Becuase I want to create the game Type the color name and change to another color. – Benz Dec 22 '17 at 10:22
-
Check out the `switch` statement in Java – Stefan Dec 22 '17 at 10:35
-
Possible duplicate of [Android: Set Random colour background on create](https://stackoverflow.com/questions/25923116/android-set-random-colour-background-on-create) – M at Dec 24 '17 at 20:15
1 Answers
1
There are two approaches
Using predefined colors as in
In colors.xml
<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>
In onCreate()
int[] androidColors = getResources().getIntArray(R.array.androidcolors); int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)]; view.setBackgroundColor(randomAndroidColor);
Using Random RGB coloring as in
Random rnd = new Random(); currentStrokeColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));

M at
- 1,020
- 1
- 12
- 26