Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android selectableItemBackground no glow effect on long press

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: long press animation not working

Why does selectableItemBackground not run the glow effect on KitKat? How can I get it to work on both KitKat and Lollipop?

like image 550
Ove Avatar asked Jul 12 '15 11:07

Ove


1 Answers

Use

  android:background="?attr/selectableItemBackground"
  android:backgroundTint="@android:color/holo_blue_bright"
  android:clickable="true"

in your recyleview row layout

like image 139
Libin Thomas Avatar answered Nov 15 '22 01:11

Libin Thomas