0

lets say I have a variable isBlue = true. If it is true I want to set my newly added RecyclerView item text to blue. If it is false I want to set my newly added RecyclerView item text to green.

if(isBlue){
                dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.redColor))
            }else{
                dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.greenColor))
            }
}

I have tried adding this code to onBind(), but it paints all the text to the set color, repainting the previous RecyclerView item texts, so I end up with all RecyclerView item text being either blue or green.

private inner class SubjectDateHolder(view:View) : RecyclerView.ViewHolder(view), View.OnClickListener{

        private lateinit var date : Date

        private val dateTextView :TextView = itemView.findViewById(R.id.text_view_subject_date)

        fun bind(date : Date){
            this.date = date

            dateTextView.text = SimpleDateFormat("d.MMMM  ''y. EEEE").format(this.date)

        }

    }

    private inner class SubjectDateAdapter(var dates : List<Date>) : RecyclerView.Adapter<SubjectDateHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubjectDateHolder {
            val view = layoutInflater.inflate(R.layout.list_item_date, parent, false)
            return SubjectDateHolder(view)
        }

        override fun getItemCount(): Int {
            return dates.size
        }

        override fun onBindViewHolder(holder: SubjectDateHolder, position: Int) {
            val date = dates[position]

            holder.bind(date)
        }

    }
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="#ffffff">

    <TextView
        android:id="@+id/text_view_subject_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="20sp"

        />

</androidx.constraintlayout.widget.ConstraintLayout>
Jakicc
  • 47
  • 7
  • Please don't tag questions with the android-studio tag just because you use it: the Android Studio tag should **only** be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. See [when is it appropriate to remove an IDE tag](https://meta.stackoverflow.com/a/315196/6296561), [How do I avoid misusing tags?](https://meta.stackoverflow.com/q/354427/6296561), and [the tagging guide](/help/tagging). Use [android] or other relevant tags instead. – Zoe Apr 17 '20 at 11:47
  • Can you provide the whole layout? (R.layout.list_item_date) – FuriousSpider Apr 17 '20 at 11:53
  • When you write code in onBind() it reflects on each item in recyclerview whether there is a conditional code or not – Jaimil Patel Apr 17 '20 at 11:57
  • @FuriousSpider Updated the with the layout file. Its a simple list item xml that I am using in my RecyclerView. – Jakicc Apr 17 '20 at 12:53
  • I think you could set the text color just below setting the text in your bind(date: Date) function. Just pass another argument that defines the text color. – FuriousSpider Apr 17 '20 at 13:03
  • @FuriousSpider it works if I needed to have all text the same color. But I need to color my text according to a outside variable. So some text should be blue color, some green. If I put a conditional if(isBlue){ dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.redColor)) }else{ dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.greenColor)) } } I need to set the text to the color, and make it stay that color. If you get what I mean. Some text in single RecyclerView item should be blue some green – Jakicc Apr 17 '20 at 13:11
  • @Jaki If you set color to textView you will always color the whole text inside. If you want to color only a part of the text you can use for example Spannable. Just like here: https://stackoverflow.com/questions/4032676/how-can-i-change-the-color-of-a-part-of-a-textview Hope I understood you correctly. – FuriousSpider Apr 17 '20 at 13:17
  • @FuriousSpider sorry I think you missunderstood me, sry for poor explanation. I want my RecyclerView items to be colored differently according to my Boolean variable isBlue. If isBlue= true, and i press add, RecyclerView should add a item1 with blue text. For the next item if isBlue = false RecyclerView should add a item2 with green text. And I should end up with one item being blue and other being green. My problem is, if I add that conditional in onBind(date : Date), all off my previous items in RecyclerView have its text painted the same color. – Jakicc Apr 17 '20 at 13:30
  • I have posted my answer please check here https://stackoverflow.com/a/61279348/12386821 – Jaimil Patel Apr 20 '20 at 17:58

2 Answers2

0

So you should be add one boolean key in your API according to color isBlue or isGreen so when you get Json you can write conditional code for color of text like..

if(<Your Boolean Key of Json>){
                dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.redColor))
            }else{
                dateTextView.setTextColor(ContextCompat.getColor(context!!, R.color.greenColor))
            }
}
Jaimil Patel
  • 1,301
  • 6
  • 13
0

If I understood correctly from the commentary, you want to add items to your recyclerview and want to color the new item. To do that, instead of passing a listOf<Date> to your recyclerview you need to pass a listOf<MyItem> data class with something like this:

data class MyItem(val isBlue: Boolean, val date: Date)

In doing so when you're in the holder.bind() method you can use your condition to color the text

Biscuit
  • 4,840
  • 4
  • 26
  • 54