Android: Set Random colour background on create
Asked Answered
S

6

14

What I want is when I load my app up it to randomly have a certain colored background from a predefined list of strings stored in a values xml file called colours.

What I currently have is one colour set as the background defined through the string colour code using the gui editor in eclipse.

For the life of me can't work out how to get the background to randomly pick one of the 9 strings and display it each time the activity is activated.

Guidance on this would be invaluable.

Strickman answered 18/9, 2014 at 22:1 Comment(0)
C
49

In colors.xml

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

In onCreate()

int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
view.setBackgroundColor(randomAndroidColor);
Cryology answered 18/9, 2014 at 22:27 Comment(0)
T
7

There's a much better answer than the ones provided.

If you want a Truly random color, "randomly" choosing from a res file doesn't prove to be nearly as robust.

Instead, use this code snippet:

  Random rnd = new Random();
    currentStrokeColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
Tague answered 18/9, 2015 at 18:50 Comment(1)
i got value -118692481300, out of integer?Quesenberry
S
4

I think I could find an easy approach but some how long to implement ,you choose random color from a defined array of colors and than parse that string color to your background.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <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>
<array name="rainbow">
    <item>@color/bright_pink</item>
    <item>@color/red</item>
    <item>@color/orange</item>
    <item>@color/yellow</item>
    <item>@color/chartreuse</item>
    <item>@color/green</item>
    <item>@color/spring_green</item>
    <item>@color/cyan</item>
    <item>@color/azure</item>
    <item>@color/blue</item>
    <item>@color/violet</item>
    <item>@color/magenta</item>
</array>

and than this java code

String[] array = context.getResources().getStringArray(R.array.animals_array);
String randomStr = array[new Random().nextInt(array.length)];

//here you define your layout `

LinearLayout myLayout = (LinearLayout) findViewById(R.id.that_linear);

myLayout.setBackgroundColor(Color.parseColor(randomStr));
Shag answered 18/9, 2014 at 22:12 Comment(0)
A
1

You could set a variable Random rnd = new Random(); as a random number (will generate a psuedo-random number between 0 and 1). Then you could say:

    if (rnd < 0.09) {
        //pick first colour
    } else if (rnd >= 0.09 && rnd < 0.18) {
        //pick second colour
    } else if (rnd >= 0.18 && rnd < 0.27) {
        //pick second colour    
    } else // etc etc up to 1.0 when you will have 9 options, each with an equal chance of randomly being picked

This way, the random number rnd will determine which colour the background is each time onCreate() is called.

Attitudinarian answered 18/9, 2014 at 22:8 Comment(0)
V
-1

you can use ColorGenerator class to get random color.code.

snippet given below

ColorGenerator generator = ColorGenerator.MATERIAL;

int color=generator.getRandomColor();

here you can use the view reference to set color like

mUserName.setText("Suraj");

mUserName.setTextColor(color);  //it will populate name with random color each time you open your activity
Vittle answered 16/9, 2016 at 6:52 Comment(0)
C
-3

TRY THIS METHOD SIMPLE AND EASY

private boolean isOne = false, isTwo = false, isThree = false, isFour = false, isFive = false;

if(position==0){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            isTwo = true;
        }else if(isOne){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            isOne = false;
            isTwo = true;
        }else if(isTwo){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color2));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color2));
            isTwo = false;
            isThree = true;
        }else if(isThree){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color3));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color3));
            isThree = false;
            isFour = true;
        }else if(isFour){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color4));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color4));
            isFour = false;
            isFive = true;
        }else if(isFive){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color5));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color5));
            isFive = false;
            isOne = true;
        }
Charmainecharmane answered 14/3, 2018 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.