0

I currently have an Android recyclerview and a list item for it. In the list item is a cardview for my views. I want to have random backgrounds for each card list this one: enter image description here
my cards now have a solid background and I search every where and used any code but couldn't find an example for view like example.

My list item:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view_lead"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="10dp"
    app:cardPreventCornerOverlap="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/style_lead"
        android:padding="7dp">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/option"
            android:src="@drawable/ic_option"
            android:tint="@android:color/white" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/large_margin"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.makeramen.roundedimageview.RoundedImageView
                    android:id="@+id/lead_img"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:contentDescription="@string/test_pic"
                    app:riv_border_color="@color/colorPrimary"
                    app:riv_border_width="0.1dp"
                    app:riv_corner_radius="100dp"
                    tools:src="@drawable/pic_1" />

                <TextView
                    android:id="@+id/lead_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="@dimen/standard_margin"
                    android:layout_toRightOf="@id/lead_img"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size"
                    tools:text="@string/test_name" />

            </RelativeLayout>

            <TextView
                android:id="@+id/lead_city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/standard_margin"
                android:layout_marginTop="16dp"
                android:textColor="@android:color/white"
                android:textSize="@dimen/large_font_size"
                tools:text="@string/test_city" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/standard_margin">

                <TextView
                    android:id="@+id/lead_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size"
                    tools:text="30$" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/large_margin"
                    android:layout_marginTop="16dp"
                    android:layout_toRightOf="@id/lead_price"
                    android:text="@string/per_hour"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_marginTop="16dp">

                <TextView
                    android:id="@+id/with"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/standard_margin"
                    android:text="@string/with"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size" />

                <TextView
                    android:id="@+id/lead_vehicle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_toRightOf="@id/with"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size"
                    tools:text="@string/car" />

                <ImageView
                    android:id="@+id/lead_vehicle_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_toRightOf="@id/lead_vehicle"
                    android:contentDescription="@string/car_img"
                    tools:src="@drawable/ic_car" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</android.support.v7.widget.CardView>
Idan
  • 5,079
  • 7
  • 34
  • 48

6 Answers6

4

You can generate random colors each time you put a new item in the adapter list See this


Since you only provided the xml part, I am assuming you have a model where you store the information to display. You can add another property "color" where you can store the random color for each item, and set in on your onBindViewHolder method

Ayush Khare
  • 1,750
  • 1
  • 14
  • 25
  • @A.Heydari object 'card' should be null and it is causing NullPointerException?? Debugging your code may help you to analyze it! – Darshn Jun 29 '17 at 05:32
  • Have a look at [this](https://gist.github.com/ayushkhare/f501721640ebc6cfc1b0d54fe96901ce) – Ayush Khare Jun 29 '17 at 05:37
4
CardView cardView = (CardView)findViewById(R.id.card_view_lead);
cardView.setCardBackgroundColor(getRandomColorCode());

public int getRandomColorCode(){

  Random random = new Random();

  return Color.argb(255, random.nextInt(256), random.nextInt(256),     random.nextInt(256));

}
2
CardView card = (CardView)findViewById(R.id.card_view_lead);  

in your onbindview holder

Random rnd = new Random();
        currentColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    holder.card.setCardBackgroundColor(currentColor);
Moulesh
  • 2,672
  • 1
  • 20
  • 32
  • nullpointerexaption for card.setBackgroundColor(currentColor); –  Jun 29 '17 at 05:16
  • that means you are not assigning the reference of cardview check updated answer.. Hope you know how to take reference to views from adapter – Moulesh Jun 29 '17 at 05:22
1

Create Array in res/values/colour like this

<array name="note_neutral_colors">
    <item>#9E9E9E</item>
    <item>#455A64</item>
    <item>#607D8B</item>
</array>

<array name="note_accent_colors">
    <item>#039BE5</item>
    <item>#3D51B3</item>
    <item>#689f38</item>
    <item>#FD7044</item>
</array>

In Your Adapter Class

holder.cardView.setCardBackgroundColor(noticeModel.getColor());

Model Class

//Variable
int color;
//Cunstructor
this.color=color;  
///and generate Getter Setter

Do this Your Main Class.java

private static int getRandomColor(Context context) {
    int[] colors;
    if (Math.random() >= 0.6) {
        colors = context.getResources().getIntArray(R.array.note_accent_colors);
    } else {
        colors = context.getResources().getIntArray(R.array.note_neutral_colors);
    }
    return colors[((int) (Math.random() * colors.length))];
}

call this method with string request like this

yourModel = new Your_Model(data1,data2,data3,getRandomColor(YourActivity.this));
Abhishek Singh
  • 8,508
  • 5
  • 27
  • 47
0

You need to write some logic to produce random colors and then use it like

CardView card = (CardView)findViewById(R.id.card);
card.setCardBackgroundColor(color);

To produce random colors, solutions are already available on Stack Overflow, please have a look at this link.

Alok Gupta
  • 1,323
  • 1
  • 12
  • 29
  • i tried this but i did got an error 'null pointer exception'; if you have an idea about random color in java codes send me please –  Jun 29 '17 at 05:07
0

Normally we can do like this, 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>

And do this in your onCreateView(),

CardView card_view_lead;
         card_view_lead= (CardView) findViewById(R.id.card_view_lead);

        int[] androidColors = getResources().getIntArray(R.array.androidcolors);
        int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
        card_view_lead.setBackgroundColor(randomAndroidColor);

If you don't want to use color array then you can use like this in your onCreateView() or onCreate(),

mCardViewTop.setCardBackgroundColor(getRandomColor());
public static int getRandomColor() {
        Random rnd = new Random();
        return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    }
Sunisha Sindhu
  • 1,395
  • 13
  • 37