Search code examples
androidandroid-layouttouchripple

View feedback onTouch


I want my views to be able to give feedback on touch and so I do

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground" />

However, I don't see any feedback when I touch my views.

For my specific case I have a RecyclerView with items, and I want the view of each item to give feedback. And so in the .xml of the item, I placed the background as android:background="?attr/selectableItemBackground". Is there more to it?


Solution

  • View have to be clickable so add android:clickable="true". This is auto added when you set onClickListener.

    If this doesn't help it would be nice to share your .xml resource file. It depends on what kind of states have to specified in it. http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

    Edit based on comments:

    Api <= 19:

    <selector xmlns:a="http://schemas.android.com/apk/res/android">
    <item
        a:drawable="@color/gray230"
        a:state_pressed="true" />
    
    </selector>
    

    Api > 19 (touch feedback limited to view bounds)

    <ripple xmlns:a="http://schemas.android.com/apk/res/android"
        a:color="@color/gray230">
    
    <item
        a:id="@android:id/mask"
        a:drawable="@android:color/black" />
    
    <item>
    
        <selector>
    
            <item
                a:drawable="@color/gray230"
                a:state_selected="true"/>
    
        </selector>
    
    </item>
    
    </ripple>
    

    Api > 19 (without view bounds limit)

    <ripple xmlns:a="http://schemas.android.com/apk/res/android"
    a:color="@color/gray230">
    </ripple>
    

    This is what I'm using. Usually I'm setting these resource files as background resource. This, of course, can be done with code as well.