I would like to change the color of Card Item whenever user does tap on any of the item, but nothing happens.. why ?
<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="3dp"
    android:layout_marginTop="9dp"
    android:layout_marginLeft="9dp"
    android:layout_marginRight="9dp"
    card_view:cardElevation="0.01dp"
    android:layout_marginBottom="0dp"
    android:foreground="@drawable/card_foreground">
                Please try this..
add android:clickable="true" and android:focusable="true" and 
change android:foreground to android:foreground="?android:selectableItemBackground"
<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="3dp"
    android:layout_marginTop="9dp"
    android:layout_marginLeft="9dp"
    android:layout_marginRight="9dp"
    card_view:cardElevation="0.01dp"
    android:layout_marginBottom="0dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:selectableItemBackground" >
</android.support.v7.widget.CardView>
                        in Recylerview.Adapter class use the following code,
@Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(applicationContext);
        final View view = inflater.inflate(R.layout.additem, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int itemPosition = recyclerview.getChildPosition(view);
                String item = list.get(itemPosition);
                Toast.makeText(applicationContext, item, Toast.LENGTH_LONG).show();
            }
        });
        return new ViewHolder(view);
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With