andrewedgar
andrewedgar

Reputation: 877

Wrong text color binding to Viewholder

I have a RecyclerView presenting a list of items with timestamps. I want the text of one of the TextViews in each viewholder to be a different color based on that timestamp of each item. The Viewholder appears to be binding properly in the logs, and the correct colors are showing in the left hand pane in Android Studio,but the text colors on the device are wrong:

enter image description here

Here is the relevant code for my adapter:

 @Override
    public void onBindViewHolder(@NonNull ChatViewHolder holder, int position) {

    Log.d(TAG, "OnbindViewholder Called");
    Message message = messageList.get(position);
    int authorColor = getAuthorColor(Calendar.getInstance().getTime(), message.getTimestamp());

    Log.d(TAG,Integer.toString(authorColor));

    holder.message.setText(message.getMessage());
    holder.author.setText(message.getAuthor() + ":");
    holder.author.setTextColor(authorColor);

}

public class ChatViewHolder extends RecyclerView.ViewHolder {

    TextView author;
    TextView message;
    RelativeLayout singleMessageContainer;

    public ChatViewHolder(View itemView) {
        super(itemView);

        author = itemView.findViewById(R.id.chatAuthor);
        message = itemView.findViewById(R.id.chatMessage);
        singleMessageContainer = itemView.findViewById(R.id.singleMessageContainer);
        author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createDMActivity(author.getText().toString());
            }
        });

    }
}

private int getAuthorColor(Date currentDate, Date messageDate) {
    int authorColorResourceID = 1;
    long timeAgo = currentDate.getTime() / 60000 - messageDate.getTime() / 60000;

    Log.d(TAG, "Time Ago = " + Long.toString(timeAgo));

    if (timeAgo < 15) {
        authorColorResourceID = R.color.Accent3;

    }
    if (timeAgo >= 15 && timeAgo < 30) {
        authorColorResourceID = R.color.chatLast30;
    }

    if (timeAgo >= 30 && timeAgo < 60) {
        authorColorResourceID = R.color.chatLastHour;
    }
    if (timeAgo >= 60 && timeAgo < 180) {
        authorColorResourceID = R.color.chatLast3Hours;
    }
    if (timeAgo >= 180) {
        authorColorResourceID = R.color.chatOver3Hours;
    }

    Log.d(TAG, Integer.toString(authorColorResourceID));


    return authorColorResourceID;

}

And my colors.xml file:

<resources>
<color name="colorPrimary">#6247aa</color>
<color name="colorPrimaryDark">#1b1721</color>
<color name="colorAccent">#bdede0</color>

<color name="yellow">#f1c40f</color>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
<color name="green">#27AE60</color>
<color name="light_purple">#E8E1E7</color>
<color name="light_green">#50d050</color>
<color name="dark_green">#008000</color>
<color name="Accent">#bdede0</color>
<color name="lightBackground">#e0e0e0</color>
<color name="text_color_primary">#ddffffff</color>
<color name="hintColor">#999999</color>
<color name="Accent2">#bb90fe</color>
<color name="Accent3">#55d6be</color>
<color name="chatLast30">#3f9b89</color>
<color name="chatLastHour">#2e7164</color>
<color name="chatLast3Hours">#24584e</color>
<color name="chatOver3Hours">#1c413b</color>

Upvotes: 1

Views: 540

Answers (1)

Ben P.
Ben P.

Reputation: 54204

TextView.setTextColor() accepts a color value. You are passing it a color resource id. It is unfortunate that both are represented as an int, so the compiler can't tell that this mistake is being made.

To fix, resolve the color resource id to a color value before calling setTextColor():

int authorColorId = getAuthorColor(Calendar.getInstance().getTime(), message.getTimestamp());
int authorColor = ContextCompat.getColor(holder.itemView.getContext(), authorColorId);
holder.author.setTextColor(authorColor);

Upvotes: 3

Related Questions