android.view.InflateException when using selectableItemBackground

As @Artjom says problem is with passing application context while creating custom view. I was also facing the same issue and I was passing getApplicationContext() instead of the activity as context. After passing Activity as a context problem resolved.

Hope it might help to someone else.


Unfortunately, other answers are not my case. The way to work for me is to check if your activity uses AppCompat theme style:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>

Then use AppCompat theme in your activity (in many ways, but I show the way to use Manifest):

<application
        android:name=".Abbott"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    <!-- Any configuration inside -->

</application>

The official answer - you can't use theme attributes for android:drawable or android:src attributes. Simply not possible, by design. There's another answer that explores the same issue and offers a solution: How to reference style attributes from a drawable?

In my case, I couldn't really do that since AppCompat's ?selectableItemBackground references a private drawable. So I opted for a code solution - I'm setting the foreground to ?selectableItemBackground while the card is not selected, and to my own state list drawable once I'm in selection mode.

This is a very ugly solution, so ugly that I'm not going to share the code here, but the best I could come up with so far. If somebody has a better solution, I'd love to hear it.