0

I use a RecyclerView and a simple item layout consisting of a ConstraintLayout holding the text elements of each item. I want to set the background of all items while

  1. showing a ripple effect when clicked.
  2. visualizing when the view is active.
  3. being able to set the colors. (optional, if it really has to be)

The minimum SDK version is 21 and is allowed to be raised if required.


What I tried:

  • Using @android:drawable/list_selector_background is not customizable and did not show a ripple.
  • Using ?selectableItemBackground or ?selectableItemBackgroundBorderless does not work, it throws an exception at runtime (Failed to resolve attribute [...]). I do have the design support library placed in my gradle script, or now the com.google.android.material package. Prepending android:attr/ produced the same error.
  • Using a StateListDrawable to build it all by myself using many <ripple> Drawables seemed overly complicated since I did not want to reproduce the whole functionality of above features.
Felix D.
  • 786
  • 1
  • 11
  • 17
  • Is this what you are looking for? https://stackoverflow.com/a/30556964/6287910 – Cheticamp Oct 12 '18 at 19:56
  • This seems to be part of a suitable solution. I will give it a try and report back. Getting `?selectableItemBackground` to work would be nicer though. – Felix D. Oct 13 '18 at 12:20
  • It looks like `?selectableItemBackground` can work by setting a style attribute. The same link I gave you tells you how. I didn't mention this because you seem to want several different colors to be available. This would changes the color, but it would still be just one color. – Cheticamp Oct 13 '18 at 12:49

1 Answers1

0

This answer linked by @Cheticamp combined with the approach in this article worked fine:

The layout of an item looks like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_item_background_active_handler">

    <include
        layout="@layout/the_actual_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?selectableItemBackground" />

</FrameLayout>

And the list_item_background_active_handler looks like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/listBackgroundActive" android:state_activated="true" />

    <!-- default -->
    <item android:drawable="@android:color/transparent" />

</selector>

This allows for handling the active state with a custom color. Simply call isActivated(true) on the view of an item in onBindViewHolder() in the adapter.

The color of the ripple effect can be customized with this approach.

Felix D.
  • 786
  • 1
  • 11
  • 17