0

enter image description here

Card view random color like above image in android studio. if more card item than continue color sequence

Payal Sorathiya
  • 234
  • 1
  • 12

1 Answers1

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