There is a ScrollView in which I add a View, I need to be able to select the nested element (TextView) for these View. I made a selector, it responds to pressing, and changes color, but when I release it - it takes on the same appearance (color). Question: how to keep the item in the "pressed" state?

item_in_scroll_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="5dp" android:paddingTop="5dp" android:paddingLeft="10dp"> <TextView android:id="@+id/lblMsgFrom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12dp" android:textColor="@color/grey" android:textStyle="italic" android:padding="5dp"/> <TextView android:id="@+id/txtMsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" android:layout_marginRight="80dp" android:textColor="@color/grey" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:background="@drawable/chat_massage_slector"/> </LinearLayout> 

selector.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- pressed --> <item android:drawable="@color/colorRed" android:state_pressed="true"/> <!-- focused --> <item android:drawable="@color/colorRed" android:state_focused="true"/> <!-- default --> <item android:drawable="@color/bg_message_from"/> </selector> 
  • one
    Tried to add android:checable="true" for TextView and android:state_checked="true" to selector? - YurySPb
  • @YuriSPb tried, got the error android: checable = "true", says that there is no such attribute. - Kirill Stoianov
  • one
    And if clickable ? - YurySPb
  • Yes, you need to enable clickable, it didn’t work without it! - Kirill Stoianov

1 answer 1

Solved the problem by adding the desired state android: state_selected = "true" to selector:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- pressed --> <item android:drawable="@color/colorRed" android:state_pressed="true"/> <!-- focused --> <item android:drawable="@color/colorRed" android:state_focused="true"/> <!-- selected --> <item android:drawable="@color/actionBar_yellow" android:state_selected="true"/> <!-- default --> <item android:drawable="@color/bg_message_from"/> </selector> 

And in the code added a listener for TextView:

  txtMsg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { txtMsg.setSelected(!txtMsg.isSelected()); } }); 

textview code

 <TextView android:id="@+id/txtMsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" android:layout_marginRight="80dp" android:textColor="@color/grey" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:clickable="true" android:background="@drawable/chat_massage_slector"/>