Rohmatul Laily
Rohmatul Laily

Reputation: 481

Clickable false in cardview android can't work

I have a cardview in recyclerview, if it has been answered, it can't be clicked anymore, I try to use isclickable false, but can still be clicked

this is my code

val answer = kategori.answer
    if(answer.equals("answered")){


 holder.card_kategori.setCardBackgroundColor(Color.parseColor("#EF9A9A"))
        holder.card_kategori.setEnabled(false)
        holder.card_kategori.setClickable(false)
    }else{
        holder.card_kategori.setClickable(true)
        holder.card_kategori.setEnabled(true)
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    card_view:cardCornerRadius="6dp"
    android:id="@+id/card_kategori"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardElevation="3dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardPreventCornerOverlap="false">

    <TextView
        android:id="@+id/kategori_soal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:padding="10dp"
        android:layout_gravity="center"
        android:text="Approval Layout"
        android:textSize="@dimen/textsize_big"
        android:textStyle="bold"
        android:fontFamily="@font/lato"/>
</android.support.v7.widget.CardView>

Upvotes: 1

Views: 595

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You should use isClickable & isEnabled

Matcher isClickable () Returns a matcher that matches Views that are clickable.

Matcher isEnabled () Returns a matcher that matches Views that are enabled.

Finally

if(answer.equals("answered")){

         holder.card_kategori.setCardBackgroundColor(Color.parseColor("#EF9A9A"))
         holder.card_kategori.isEnabled=false
         holder.card_kategori.isClickable = false
    }else{
         holder.card_kategori.isClickable = true
        holder.card_kategori.isEnabled=true
    }

Upvotes: 1

alireza daryani
alireza daryani

Reputation: 835

you can use method below for your job:

fun View.setAllEnabled(enabled: Boolean) {
isEnabled = enabled
if (this is ViewGroup) children.forEach { child -> 
child.setAllEnabled(enabled) 
}
}

Explain :

you must get all childs of your view and set clickable of all of them false/true this method help you for get all childs and set clickable to what you want.

there is a second way for you :

you can use the set on click in recyclerview just when u want, its a manual way but will work for you.

keep me update with your issue.

Upvotes: 0

Related Questions