-2

Suppose I have a cardView Layout which have a TextView and ImageView. I showed them in RecyclerView. If I click on the view the textColor and the drawable color will become red. and then if I click another view, that clicked view will be red and prevoius will convert to black. I did the same thing in bottomNavigationView using custom color.

Here is the layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_layout_card_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="10dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="2dp"
    app:cardMaxElevation="3dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/item_image_view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/item_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/item_image_view"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"
            android:gravity="center"
            android:text="Item"
            android:textColor="#000000"
            android:textSize="12sp" />

    </RelativeLayout>

</android.support.v7.widget.CardView>
Aminul Haque Aome
  • 2,261
  • 21
  • 34
  • 4
    Possible duplicate of [Change clickable TextView's color on focus and click?](https://stackoverflow.com/questions/5371719/change-clickable-textviews-color-on-focus-and-click) – Gokul Nath KP Aug 26 '18 at 06:03
  • https://stackoverflow.com/questions/1219312/android-selector-text-color – Jitendra A Aug 26 '18 at 06:03

1 Answers1

0

You want to change the color of the recycler view row

In your adapter define variable like

var coloredIndex = -1

In your onBindViewHolder() function

h.var.setOnClickListener {
   coloredIndex = curPos
}  
changeSelectedItemColor(h, curPos)

Define this below function to change the color.

private fun changeSelectedItemColor(h: YourViewHolder, curPos: Int) {
    if (coloredIndex == curPos) {
        h.var.setBackgroundColor(ContextCompat.getColor
        (context, R.color.red))
    } else {
        h.var.setBackgroundColor(ContextCompat.getColor
        (context, R.color.black))
    }
    notifyDataSetChanged()
}

This is a example. You didn't add your recycler view adapter code in your question. Please add it for further clarification.

Shaon
  • 2,496
  • 26
  • 27