AMIT KUMAR KARN
AMIT KUMAR KARN

Reputation: 375

Changing foreground color of Recycler view when an item is selected

How to change foreground color of Recycler view when an item is selected ?

e.g All other items except the selected item needs to be with different foreground color.Google allo assistant

Upvotes: 0

Views: 478

Answers (1)

Khemraj Sharma
Khemraj Sharma

Reputation: 58934

You can do it with drawable selector. Put bg.xml in drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background_selected" android:state_selected="true"/>
    <item android:drawable="@color/background_unselected"/>
</selector>

Now set this instead of your background.

<TextView
    ....
    android:background="@drawable/bg"
    ...
    />

You just need to set selected in your adapter.

textView.setSelected(true);

Upvotes: 1

Related Questions