Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView Click state not working [closed]

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">
like image 365
Oreo Avatar asked May 26 '15 05:05

Oreo


2 Answers

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>
like image 64
Rohit Suthar Avatar answered Nov 18 '22 12:11

Rohit Suthar


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);
    }
like image 38
balu b Avatar answered Nov 18 '22 11:11

balu b