I am trying to use the RecyclerView in an Android app. I imported the RecyclerView sample project into Android Studio, and it works fine.
I want to make the items in the recycler view to react visually when the user long presses them. In order to do that, I set the background of the view to selectableItemBackground
. I made these modifications:
inside text_row_item.xml, I added this line on the FrameLayout tag:
android:background="?android:attr/selectableItemBackground"
inside CustomAdapter.java, I added a long click listener on the FrameLayout:
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.d(TAG, "Element " + getPosition() + " long clicked.");
return true;
}
});
If I run the modified sample on a Lollipop emulator, I get the nice ripple effect when I long press the items in the recycler view.
But if I run the app on a KitKat emulator, I don't get the glow effect that comes with KitKat. The item in the list gets darker when I long-press it, and the long click listener does run, but I expected the color to glow as the long press was happening.
Here is a gif of what I'm seing:
Why does selectableItemBackground
not run the glow effect on KitKat? How can I get it to work on both KitKat and Lollipop?
Use
android:background="?attr/selectableItemBackground"
android:backgroundTint="@android:color/holo_blue_bright"
android:clickable="true"
in your recyleview row layout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With