selectableItemBackgroundBorderless が効かない場合の対処法


概要

Viewのサイズをはみ出した ripple エフェクトを適用したい時に便利な selectableItemBackgroundBorderless は、特に親のViewGroupが ConstraintLayout である場合に ripple が出ないといった意図しない挙動をすることがあります。

根本的な解決策を見つけるまでに時間がかかったので、自分用に調査したメモをまとめておきます。

結論

selectableItemBackgroundBorderlessが効かない場合は、selectableItemBackgroundBorderlessを適用しているViewを android:background="@android:color/transparent"を設定したFrameLayout でラップする。

なぜそうなるのか

StackOverflowの回答に的確な説明があったので、重要な部分の抄訳をしておきます。

Explanation:
"Borderless buttons draw their content on the closest background. Your button might not be having background between itself and the ImageView, so it draws underneath the ImageView."

ボーダーレスボタンのコンテンツは最も近いbackgroundに描画されます。ボタンとそのそばにある ImageView の間にはbackgroundが存在しておらず、結果として ImageView の下に描画されます。

参考