Card view random color like above image in android studio. if more card item than continue color sequence
Asked
Active
Viewed 1,019 times
0
-
1This will generate random color code http://stackoverflow.com/questions/5280367/android-generate-random-color-on-click so you can assign that color code to the card – Android Surya Aug 02 '16 at 10:09
-
it's work @Surya Bondada – Payal Sorathiya Aug 02 '16 at 10:29
-
@ Payal Sorathiya welcome. you can vote for my comment now. – Android Surya Aug 02 '16 at 10:31
1 Answers
2
Define colours of your choice like these in colors.xml file
<?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>
<integer-array name="androidcolors">
<item>@color/blue</item>
<item>@color/purple</item>
<item>@color/green</item>
<item>@color/orange</item>
<item>@color/red</item>
</integer-array>
</resources>
To randomise selection of colours you could use the following code:
int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
view.setBackgroundColor(randomAndroidColor);

not_again_stackoverflow
- 1,285
- 1
- 13
- 27