Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-L CardView Visual Touch Feedback

could anybody explain to me how to implement some of the visual touch feedback that was demonstrated at Google I/O 2014 within a CardView.

Here is how I am using the CardView in XML, there is probably something small that I am missing, so I just wondered if anyone could help me?.

<!-- A CardView --> <android.support.v7.widget.CardView     xmlns:card_view="http://schemas.android.com/apk/res-auto"     android:id="@+id/CardView_1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_marginLeft="10dp"     android:layout_marginRight="10dp"     android:layout_marginTop="10dp"      card_view:cardCornerRadius="4dp"     android:elevation="2dp">      <LinearLayout         android:id="@+id/LinearLayout_1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal"         android:onClick="RunSomeMethod"">      <!-- Main CardView Content In Here-->      </LinearLayout> </android.support.v7.widget.CardView> 
like image 565
Smiler Avatar asked Jun 29 '14 10:06

Smiler


People also ask

What is CardView Android?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.

What is cardUseCompatPadding?

What is cardUseCompatPadding? For a more consistent UI, use cardUseCompatPadding=true. CardView adds additional padding to draw shadows on platforms before Lollipop. This may cause Cards to have different sizes between Lollipop and before Lollipop.


2 Answers

API 11+:

Add android:foreground="?android:attr/selectableItemBackground" to your CardView element.

API 9+:

Add android:foreground="?selectableItemBackground" to your CardView element.


Edit: The ripple originating from the center and not from the touch point is a known bug, and has been fixed.

like image 61
nhaarman Avatar answered Sep 28 '22 23:09

nhaarman


To draw selection on pre-Lollipop and post-Lollipop correctly you can use the following approach (the idea is to use inset drawable of selector with rounded corners for pre-Lollipop - sample below uses custom colors, you can change them to default):

android:foreground="@drawable/card_foreground" 

post-Lollipop

drawable-v21/card_foreground.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#20000000"         android:drawable="@drawable/card_foreground_selector" /> 

drawable-v21/card_foreground_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true">         <shape android:shape="rectangle">             <solid android:color="#18000000"/>         </shape>     </item>     <item android:state_focused="true" android:state_enabled="true">         <shape android:shape="rectangle">             <solid android:color="#0f000000"/>         </shape>     </item> </selector> 

pre-Lollipop

drawable/card_foreground.xml (you'll need to tweak inset values according to elevation of your card)

<inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/card_foreground_selector"     android:insetLeft="2dp"     android:insetRight="2dp"     android:insetTop="4dp"     android:insetBottom="4dp"/> 

drawable/card_foreground_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true">         <shape android:shape="rectangle">             <solid android:color="#18000000"/>             <corners android:radius="@dimen/card_radius" />         </shape>     </item>     <item android:state_focused="true" android:state_enabled="true">         <shape android:shape="rectangle">             <solid android:color="#0f000000"/>             <corners android:radius="@dimen/card_radius" />         </shape>     </item> </selector> 
like image 25
GregoryK Avatar answered Sep 28 '22 22:09

GregoryK