Home > Enterprise >  I want to change the background color of checkbox button at the time of focus in android
I want to change the background color of checkbox button at the time of focus in android

Time:04-01

I want to change the color of check box button as when it gets focus its background color will change and when it has no focus it will remain the same.

I have tried using selector but not able to change it can any one help me out in this

CodePudding user response:

You sould use checkbox change listener. I think that is enough for this process but there are other optsions ofcourse

var checkBox:CheckBox = CheckBox(context)
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
    if (isChecked) {
            //Do Whatever you want in isChecked
    }
}

CodePudding user response:

Use this:

Backround-color: 'any color'

CodePudding user response:

Create drawable file like this :

checkbox_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/pink"/>
    <item android:state_checked="false" android:color="@color/checkbox_not_selected"/>
</selector>

Apply it like this :

<androidx.appcompat.widget.AppCompatCheckBox
                android:id="@ id/chkSelected"
                android:layout_width="@dimen/_23sdp"
                android:layout_height="@dimen/_23sdp"
                android:layout_alignParentEnd="true"
                android:buttonTint="@drawable/checkbox_color"
                android:checked="false"
                android:clickable="false" />

then change it runtime like this to change color runtime based on of it is selected :

chkSelected.isSelected = true

or

chkSelected.isSelected = false
  • Related