3

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.

K. Yen
  • 193
  • 2
  • 14
  • Is any specific requirement to define color in attrs.xml ? – J Ramesh Oct 13 '17 at 03:42
  • Hi, yes. I assume it is easier to manage the code in this way as I just take the reference color from the attrs.xml file – K. Yen Oct 13 '17 at 03:49
  • I would suggest you to use styles. Declares 2 styles: `primaryText` & `secondaryText` in which you declare the text color of the message. From the code you can change the style of the textView. Please see [here](https://developer.android.com/guide/topics/ui/look-and-feel/themes.html) – Luca Nicoletti Oct 13 '17 at 08:27
  • @LucaNicoletti Hi, I dont want to set the colour in the resource stylesheet as it will not be dynamic. The requirement is that the textcolour will automatically change once there is any update to my RecyclerView which I feel that setting it in the code is the best way to go. What I mean by dynamic here is that, if there is a new message comes in, then the recyclerView will show the new message on top. If I set the colour in the stylesheet, the textColor will not change according to the priority if I did not set in the code – K. Yen Oct 13 '17 at 08:45
  • You can change the theme in code – Luca Nicoletti Oct 13 '17 at 08:46
  • @LucaNicoletti, wouldn't that be redundant though, to create another stylesheet? Since I only want to set the default textcolor which is aldi in the android attrs.xml But thanks for your advice, I will try it if I still couldn't get the code to work – K. Yen Oct 13 '17 at 09:31
  • Seems like problem is with accessing values from attribute file. Ref https://stackoverflow.com/questions/3441396/defining-custom-attrs – VVB Oct 13 '17 at 10:03

0 Answers0