Home > Software engineering >  How to change the color of check and border in checkbox
How to change the color of check and border in checkbox

Time:11-16

Want to do

I want to make checkbox with 4 patterns. To achieve this, I want to know,

① How to change the color of check.

② How to change the color of frame border of checkbox.

I already tried this code and changed the background's color. However it doesn't work enough to achieve what I want.

//change the color of background in a checkbox
android:buttonTint="#ffffff"

CodePudding user response:

You need to manually define various attributes in xml. Here is an example - you need to modify it to your specific use case:

          <CheckBox
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:button="@null"
            android:id="@ id/checkbox"
            android:background="?android:attr/listChoiceIndicatorMultiple"
            android:colorAccent="@color/checkboxcolor"
            android:buttonTint="@color/checkboxcolor" />

You can of course change the android:background to something else. Also notice the android:button can be any drawable, but I am not using it and then set it to @null. So you should be able to achieve your desired result by working with these xml attributes such as the background (any drawable - here I am just using a built in drawable from android, but you can use your own drawable. You can then have a border defined inside that custom drawable if you want.

CodePudding user response:

In general, you can use property android:button to achieve this.

Please refer to the following code:

1.create a xml file checkbox_background.xml in folder drawable

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

2.set the android:button property of CheckBox to checkbox_background:

  <CheckBox
   android:id="@ id/check_box"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:button="@drawable/checkbox_background" />

Note: You can also set other status of checkbox in the item of selector .

  • Related