How to change the color of a ListView item while it is being pressed?

1

By default, when you click an item, it changes color at the time of the click, however I've changed my listView and this click is not working. Does anyone know how to implement it if it is some view-ready method, an animation or some check with a setBackground? I tried doing a check but it did not work as it should. Thank you in advance.

I need something like this in this image, but when the user drop the item back to normal color.

    
asked by anonymous 12.11.2015 / 19:34

2 answers

2

Create a selector for your list, with the desired colors for clicks, longClicks, etc:

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

Then, call your selector inside your ListView:

<ListView android:id="@+id/list" 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:listSelector="@drawable/meuSeletor" />
    
23.11.2015 / 03:40
1

Daniel, how are you?

Follow the link from the official Google documentation regarding this feature. It explains well what each attribute does and gives an example at the end. Here are some excerpts from the page:

Create the file res / color / button_text.xml with the content below.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
      android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
      android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>

And here is an example implementation, note that in the android:textColor attribute we refer to the res/color/button_text.xml we did above:

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />

Documentation page link here.

    
09.06.2016 / 21:10