cyborg
cyborg

Reputation: 15

How to show random colors as a button background of our choice

i am working on app for kids.. i need that a button background changes randomly...with specific colors given by me. and when user click the button toast shows the color name at that moment.

example: you clicked on color: RED

i know the use of random function but how to make changes to this function that it shows colors of my choice.

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
view.setBackgroundColor(color);

color list:

<color name="bright_pink">#FF007F</color>
<color name="red">#FF0000</color>
<color name="orange">#FF7F00</color>
<color name="yellow">#FFFF00</color>
<color name="chartreuse">#7FFF00</color>
<color name="green">#00FF00</color>
<color name="spring_green">#00FF7F</color>
<color name="cyan">#00FFFF</color>
<color name="azure">#007FFF</color>
<color name="blue">#0000FF</color>
<color name="violet">#7F00FF</color>
<color name="magenta">#FF00FF</color>

how to force random function to use only above colors.

Upvotes: 0

Views: 2199

Answers (4)

iamreptar
iamreptar

Reputation: 1451

Create an array of the colors you want to randomize.

<resources>
    <string-array name="color_names">
        <item>red</item>
        <item>green</item>
        ...
    </string-array>
</resources>

Get the random color

Random random = new Random();
String[] colorArray = context.getResources().getStringArray(R.array.color_names); 
String randomColorName = colorArray[random.nextint(colorArray.length())];
String randomColorResource = "R.color." + randomColorName;

Set your button color.

findViewById(R.id.myButton).setBackgroundColor(randomColorResource);

Create toast message with the name of the color.

Toast.makeText(getApplicationContext(), 
               randomColorName, 
               Toast.LENGTH_SHORT).show();

Upvotes: 1

Aswin
Aswin

Reputation: 1204

This might helps you.

Create a list of colors.

List<String> colors = new ArrayList<>();

colors.add("#00ff00"); //eg.Blue
colors.add("#ff0000"); // ...
colors.add("#ff00ff");
...

Get Random object.

Random random = new Random(0);

Now get the random value generated within bound of your list size.

int generatedRandomNum = random.nextInt(colors.size());

Now get the random color and set it to the view.

view.setBackgroundColor(Color.parseColor(colors.get(generatedRandomNum)));

Based on the generatedRandomNum (i.e. position of array) Toast the color name that you already noted like Blue.

Note: Random might generate same number in a sequence. To know more about random check this. If you don't want to repeat the same value, you should put some more logic behind it.

Upvotes: 0

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Do this way

Field[] fields = R.color.class.getDeclaredFields(); 

    int[] colors = new int[fields.length]; // Make this variable global

    try {
        for (int i = 0; i < fields.length; i++) {
            colors[i] = fields[i].getInt(null);
        }
    } catch (Exception ex) {
    }


    int color = colors[new Random().nextInt(colors.length-1)];
    view.setBackgroundColor(color);

Upvotes: 0

Prateek
Prateek

Reputation: 4013

Why don't you create an Array of color codes and use it to pick random color codes of your choice. Or if you want to create an array in your resource file then you can also go by that approach.

colors.xml

<resources>
    <string-array name="colors">        
        <item>#ff0000</item>
        <item>#00ff00</item>  
        <item>#0000ff</item>
    </string-array>
</resources>

Code in activity class.

String[] allColors = context.getResources().getStringArray(R.array.colors);

Color.parseColor(allColors[0]) // red
Color.parseColor(allColors[1]) // green
Color.parseColor(allColors[2]) // blue

Upvotes: 1

Related Questions