Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output a random color from a set of selected colors in Java? (Android)

So I want a string to be given a random color any time the user inputs an answer. My issue is that I'm not sure how to make that random color of a string be a color of a specific range. For example, if I wanted the string to be randomly become blue, red, green, pink, white, or brown. Only these colors, none other.

So far I have managed a completely random color out of all possible RBG variations using the following code:

Random rand = new Random();
            int r = rand.nextInt(254)+1;
            int g = rand.nextInt(254)+1;
            int b = rand.nextInt(254)+1;

            int randomColor = Color.rgb(r,g,b);
            word.setTextColor(randomColor);

Though as previously mentioned, this is not what I aim to achieve. Instead of this, I want set colors that can be randomly applied to the string. These are colors that I would pick, then have randomly set as the string color. This sets a completely random color out of a range I do not intend to have. I could end up with 5 variations of blue for example.

If anyone could put forward a solution, I'd appreciate it. Thank you.

like image 431
H3ll0 Avatar asked Mar 10 '23 19:03

H3ll0


2 Answers

An easy way to do this would be to define a LIST of colors. Then use a random to to select one of the colors on your action.

Create List of Desired Colors

This is not an extensive list but includes 4 colors to start, RED, Blue, Green and Pink.

List<Integer> colors = new ArrayList<>();
colors.add(Color.rgb(235,22,220));
colors.add(Color.BLUE);
colors.add(Color.GREEN);
colors.add(Color.RED);

Create a simple method to return a random color

Now we just need a method to return a random color from our list. It is a whopping 2 lines long.

private int randomColor(){
    Random rand = new Random();
    return colors.get(rand.nextInt(colors.size()));
}

Change textview color

Now we can just change the color. An example below is in a button onClickListener. You can also add in some validation here to make sure you don't get a duplicate color, say blue twice in a row.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int color = randomColor();
        if(txtView.getCurrentTextColor() != color){
             txtView.setTextColor(color);
        }else{
             txtView.setTextColor(randomColor());
        }
    }
});

Easy, clean and quick to write.

like image 21
basic Avatar answered Apr 28 '23 04:04

basic


First of all in color.xml define your colors and create array of it.

<?xml version="1.0" encoding="utf-8"?>
<resources>

<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>

</resources>

Now generate random color like below in onCreate method.

int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];

Lastly set this generated color.

view.setBackgroundColor(randomAndroidColor);  

Source taken from here.

like image 148
Akash Patel Avatar answered Apr 28 '23 04:04

Akash Patel