Home > database >  I am using card view with in recycler view and i want to change color of each single card
I am using card view with in recycler view and i want to change color of each single card

Time:01-07

I am using card view within recycler view and I want to change the color of each single card view means Every card view has different color like in the picture I'm using retrofit technique for fetching data. please tell me where to put what code as I'm new in java android TIA

enter image description here

This is my adapter class

 public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{

    private Context context;
    private List<Corona> dataList;

    public DataAdapter(Context context, List<Corona> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.data,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.nameTv.setText(dataList.get(position).getName());
        holder.cityTv.setText(dataList.get(position).getCity());
        

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        public TextView nameTv, cityTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            nameTv = (TextView) itemView.findViewById(R.id.tvName);
            cityTv = (TextView) itemView.findViewById(R.id.tvCity);
        }
    }
}

here is XML of Card View

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardBackgroundColor="#EDCC1A"
        app:cardCornerRadius="15dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <TextView
                android:id="@ id/tvName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Hello World!"
                android:textSize="25sp"
                android:textStyle="bold"
                android:textColor="#F00"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@ id/tvCity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="#0000FF"
                android:textSize="25sp"
                android:textStyle="bold"
                android:layout_below="@ id/tvName"
                android:text="Hello World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </RelativeLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

here is my main activity

  public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private DataAdapter dataAdapter ;
    private List<Corona> dataArrayList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews(){
 

   recyclerView=(RecyclerView) findViewById(R.id.recview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    getCoronaData();
    }


    private void getCoronaData() {

        ApiInterface apiInterface;
        apiInterface = ApiClient.getClient().create(ApiInterface.class);
        dataArrayList = new ArrayList<>();

        Call<List<Corona>> call = apiInterface.getCoronaDataForParsing();
        call.enqueue(new Callback<List<Corona>>() {
            @Override

            public void onResponse(Call<List<Corona>> call, Response<List<Corona>> response) {
                dataArrayList = response.body();
                dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
                recyclerView.setAdapter(dataAdapter);
            }

            @Override
            public void onFailure(Call<List<Corona>> call, Throwable t) {

            }

        });

    }
}

CodePudding user response:

First of all set an ID to cardView in your XML file, then initialize cardView in MyViewHolder :

CardView cv = (CardView) itemView.findViewById(R.id.YOUR_CARDVIEW_ID);

now go to onBindViewHolder and add this code :

holder.cv.setCardBackgroundColor(YOUR_COLOR);

CodePudding user response:

first create integer.xml file under /res/values like below: #Add as many color items as your want.

integer.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="color_array">
    <item>@color/color1</item>
    <item>@color/color2</item>
    <item>@color/color3</item>
    <item>@color/color4</item>
    <item>@color/color5</item> 
</integer-array>

Then, Inside RecyclerViewAdapter pass it through the constructor like below:

 public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{
  private Context context;
  private List<Corona> dataList;
  private int[] colorList;

  public DataAdapter(Context context, List<Corona> dataList){
      this.context = context;
      this.dataList = dataList;
      colorList = context.getResources().getIntArray(R.array.color_array);
  }
 ........
  @Override
  public void onBindViewHolder(.....){
    ......
    //for setting the different colors to each cardview.
    GradientDrawable drawable = (GradientDrawable) holder.cardLayout.getBackground();
    drawable.setColor(colorList[position % colorList.length]);

  }
   public class MyViewHolder extends RecyclerView.ViewHolder{
      CardView cardLayout;
      .........
      public MyViewHolder(View itemView){
        super(itemView)
        .........
        cardLayout = itemView.findViewById(R.id.cardLayout);
      }
   }
}

And, finally Inside XML of CardView provide id to Cardview as follow:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.cardview.widget.CardView
    android:id="@ id/cardLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:cardBackgroundColor="#EDCC1A"
    app:cardCornerRadius="15dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">

        <TextView
            android:id="@ id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Hello World!"
            android:textSize="25sp"
            android:textStyle="bold"
            android:textColor="#F00"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@ id/tvCity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="#0000FF"
            android:textSize="25sp"
            android:textStyle="bold"
            android:layout_below="@ id/tvName"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </RelativeLayout>

</androidx.cardview.widget.CardView>
  •  Tags:  
  • Related