Payal Sorathiya
Payal Sorathiya

Reputation: 244

Card view rendom color like above image in android studio

enter image description here

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

Upvotes: 0

Views: 1055

Answers (1)

not_again_stackoverflow
not_again_stackoverflow

Reputation: 1323

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

Upvotes: 2

Related Questions