Hendra Anggrian Hendra Anggrian - 3 months ago 13
Java Question

Programatically set '?selectableItemBackground' on Android view

In xml, I often do this to emulate

onClick
effect:

<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?selectableItemBackground">

...

</android.support.v7.widget.CardView>


Is there any way to access
?selectableItemBackground
in java?

Answer

for appcompat you can use,

TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
cardView.setBackgroundResource(outValue.resourceId);

check this and this answer.