Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically 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?

like image 801
Hendra Anggrian Avatar asked Jun 23 '16 09:06

Hendra Anggrian


1 Answers

For appcompat you can use,

TypedValue outValue = new TypedValue(); getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); cardView.setBackgroundResource(outValue.resourceId); 
like image 144
Amit Vaghela Avatar answered Sep 29 '22 08:09

Amit Vaghela