I have a RecyclerView where the text colour is to be set according to the priority received. Meaning if the priority is equal to 1 (which is high), the text color is RED, else the text color will follow the DEFAULT color (which is either android:textColorPrimary or android:textColorSecondary). But I have trouble getting the value from the attr file.
Below is my attempt to get it work in code RecyclerViewAdapter file
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_layout,
parent, false);
TextView projectTextView = (TextView) itemView.findViewById(R.id.text_project);
TextView timeTextView = (TextView) itemView.findViewById(R.id.text_time);
return new ViewHolder(itemView, projectTextView, timeTextView,
new ViewHolder.OnItemClickListener() {
@Override
public void onItemClick(int position) {
RecyclerViewAdapter.this.itemClickListener.onItemClick(filteredList.get(position));
}
});
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Message message = filteredList.get(position); //my Message object
holder.projectTextView.setText(message.getProject());
holder.timeTextView.setText(message.getTime());
//Get attribute colour from attr file
TypedValue typedValue = new TypedValue();
int[] attrs = new int[]{android.R.attr.textColorPrimary, android.R.attr.textColorSecondary};
TypedArray a = context.obtainStyledAttributes(typedValue.data, attrs);
int txtColorPrimary = a.getDimensionPixelSize(0, -1);
int txtSecondaryPrimary = a.getDimensionPixelSize(1, -1);
a.recycle();
//Set the text colour to red if the priority is high ("1")
if(message.getPriority().equals("1")){
holder.projectTextView.setTextColor(Color.RED);
holder.timeTextView.setTextColor(Color.RED);
}else{
//Else set the text colour to default colour
holder.projectTextView.setTextColor(txtColorPrimary); //<-- this is not working
holder.timeTextView.setTextColor(txtSecondaryPrimary); //<-- this is not working
}
}
Below is the code in attrs.xml which I am trying to get the value from
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Theme">
<!-- The most prominent text color. -->
<attr name="textColorPrimary" format="reference|color" />
<!-- Secondary text color. -->
<attr name="textColorSecondary" format="reference|color" />
<!-- Tertiary text color. -->
<attr name="textColorTertiary" format="reference|color" />
</declare-styleable>
</resources>
I only know how to set it in layout but I am not sure how to do it in java code.
android:textColor="?android:textColorPrimary"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp" android:paddingLeft="16dp"
android:paddingEnd="16dp" android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView
android:id="@+id/text_project"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="?android:textColorPrimary"
android:maxLines="1"
android:ellipsize="end"/>
<TextView
android:id="@+id/text_time"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_project"
android:layout_toEndOf="@id/text_project"
android:textStyle="italic"
android:textColor="?android:textColorSecondary"
android:maxLines="1"
android:ellipsize="end"
android:gravity="end"/>
</RelativeLayout>
I do apologise if my code seems wrong. I admit I am not really good with android code and it may seem silly to do it this way but I do appreciate the advice given in solving this.