0

I want simply highlight item when I press on it.

I have selectors below (@drawable/expandable_menu)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@color/purple_200" android:state_pressed="true" />
<item android:drawable="@color/purple_500" android:state_selected="true" />
<item android:drawable="@color/chat" android:state_activated="true" />
<item android:drawable="@color/transparent"/>
</selector>

In my onclick listener I select this item

RecyclerView XML:

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="70dp"
        android:id="@+id/chat_messages"
        app:stackFromEnd="true"
        android:clickable="true"
        android:focusableInTouchMode="true"
        android:background="@drawable/expandable_menu"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:focusable="true">

Message XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:layout_gravity="left"
>

<TextView
    android:id="@+id/chat_msg_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="this is me"
    android:textColor="@color/black"
    android:textSize="16dp"
    android:background="@color/cardview_shadow_start_color"
    android:padding="10dp"
    android:layout_alignParentTop="true" />
 </RelativeLayout>

I followed this answer (https://stackoverflow.com/a/30046476) but no success

1 Answers1

0

you have to set OnClickListener to View, which have selector set as background. I see you have selector set for TextView (child), are you setting onClick to this View or for whole itemView (which in your case is root RelativeLayout)?

you may also try to add android:duplicateParentState for TextView - this should make TextView draw pressed state when parent is pressed, not only TextView itself (which isn't clickable at all when no OnClickListener set)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I set selected state and onClickListener to holder.itemView element –  Jan 25 '21 at 09:50