Search code examples
androidandroid-recyclerviewandroid-cardview

OnTouch Listener removes round corners on CardView


I added onTouchListener to my RecyclerView adapter and it works fine with changing the color, but it also removes the round corners of RecyclerView. You can see it in the screenshot.

Here is the code:

holder.cardViewRemaining.setOnTouchListener((v, event) -> {
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            v.setBackgroundColor(Color.parseColor("#f0f0f0"));
        }
        if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
        {
            v.setBackgroundColor(Color.WHITE);
        }
        return false;
    });

before touch

after touch


Solution

  • You need to use setCardBackgroundColor() .

    holder.cardViewRemaining.setOnTouchListener((v, event) -> {
            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                holder.cardViewRemaining.setCardBackgroundColor(Color.parseColor("#f0f0f0"));
            }
            if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
            {
                holder.cardViewRemaining.setCardBackgroundColor(Color.WHITE);
            }
            return false;
        });