pulp_fiction pulp_fiction - 3 months ago 28
Android Question

Unable to set TextView background color in a RecyclerView

I have a RecyclerView which contains TextView. The holder code is below:

private class PhotoHolder extends RecyclerView.ViewHolder {
private TextView mTitleTextView;
public PhotoHolder(View itemView) {
super(itemView);
mTitleTextView = (TextView) itemView;
}
public void bindGalleryItem(GalleryItem item) {
mTitleTextView.setText(item.toString());
//--------------TEST CODE----------------
mTitleTextView.setBackgroundColor(2);
mTitleTextView.invalidate();
mTitleTextView.requestLayout();
}
}


Now, the line
mTitleTextView.setText()
works as expected but I also wanted to change background of the
TextView
. So, I tried calling the
setBackgroundColor()
method but even the call to invalidate and requestLayout don't set any background color.

Does anyone know a way to ensure setting of the background color? Is this the right place to set the color? Why is it not setting?

Also, I want to do this programmatically.

Answer

It should be

mTitleTextView.setBackgroundColor(getResources().getColor(R.color.colorOne));

or to set the Text Color you can use the below

mTitleTextView.setTextColor(getResources().getColor(R.color.colorTwo));

Define your specific color in colors.xml

<color name="colorOne">#E60000</color>
<color name="colorTwo">#D3D3D3</color>

Color code are standards across different development activities. Android framework should understand the parameter value that you are planning to set to a view. Reference Link for Graphics color code

You can use softwares similar to this to find the color code

Comments