I am trying to make a RecyclerView
with CardView
elements , each item having a different color. I am using an Adapter and passing in the resource for the color.
The problem is that my elements, instead of having the color I passed in(ex: red, blue, or green), they have different shades of purple.
If I use the same code for the child Relative Layout, the colors work perfectly, but they don't fill the whole card.
This is the element of the list:
<android.support.v7.widget.CardView
android:id="@+id/rl_main_list_background_color"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="16dp"
android:layout_marginBottom="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_main_list_game_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game"
android:layout_margin="16dp"
android:textStyle="bold|italic"
android:textSize="20sp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is the adapter
public class GamesListAdapter extends RecyclerView.Adapter<GamesListAdapter.GamesViewHolder> {
private ArrayList<GameItem> mGamesList;
public static class GamesViewHolder extends RecyclerView.ViewHolder{
public TextView mGameName;
public CardView mItemRelativeLayour;
public GamesViewHolder(@NonNull View itemView) {
super(itemView);
mGameName = itemView.findViewById(R.id.tv_main_list_game_name);
mItemRelativeLayour = itemView.findViewById(R.id.rl_main_list_background_color);
}
}
public GamesListAdapter(ArrayList<GameItem> gamesList){ mGamesList = gamesList;}
@NonNull
@Override
public GamesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_list_list_element, viewGroup, false);
GamesViewHolder gamesViewHolder = new GamesViewHolder(view);
return gamesViewHolder;
}
@Override
public void onBindViewHolder(@NonNull GamesViewHolder gamesViewHolder, int i) {
GameItem currentItem = mGamesList.get(i);
gamesViewHolder.mGameName.setText(currentItem.getGameName());
gamesViewHolder.mItemRelativeLayour.setCardBackgroundColor(currentItem.getBackgroundColor());
}
@Override
public int getItemCount() {
return mGamesList.size();
}
}
And this is how I populate the list
gamesList = new ArrayList<>();
gamesList.add(new GameItem("Truth or Dare", R.color.truthOrDareColor));
gamesList.add(new GameItem("Kings", R.color.kingsColor));
gamesList.add(new GameItem("Flip the card(Urmatoarea Carte)", R.color.flipTheCardColor));
gamesList.add(new GameItem("Most likely to", R.color.mostLikelyToColor));
gamesList.add(new GameItem("Spin the bottle", R.color.spinTheBottleColor));
gamesList.add(new GameItem("I have sex like", R.color.iHaveSexLikeColor));
gamesList.add(new GameItem("Never have I ever", R.color.neverHaveIEverColor));
Any idea on how to fix this? Thanks!