How to change the check box (tick box) color to white in android XML
Asked Answered
K

8

20

Is there any way to change the check box (tick box) color to white in android XML. (I need white color tick box which contain black tick, as the preview I got in android studio inside my real device)

Here is my code for check box

<CheckBox
    android:textSize="15sp"
    android:textColor="#fff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save Loging "
    android:id="@+id/checkBox"
    android:layout_below="@id/PasswordeditText"
    android:checked="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:buttonTint="#fff" />

When I add android:buttonTint="#fff" preview show the change I need, but it doesn't work in real device

Design preview

enter image description here

Real Device

enter image description here

Is there any attribute like android:buttonTint which I can use to achieve the changes in real device.

Kuchen answered 5/8, 2015 at 18:6 Comment(2)
Look here, may help:https://mcmap.net/q/267380/-change-checkbox-background-color-in-androidParodic
@Parodic I check with it but it doesn't work for meKuchen
D
65

Set the colorAccent to your desired color:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorAccent">#fff</item>
</style>

Or if you don't want to change your main theme, create a new theme and apply it only to the checkbox:

<style name="WhiteCheck">
    <item name="colorAccent">#fff</item>
</style>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/WhiteCheck"/>
Dancette answered 5/8, 2015 at 19:0 Comment(2)
creating alternative theme with "colorAccent" requires API level 21 and higher!Confined
@Confined colorAccent is supported by the AppCompat library. android:colorAccent would be supported by API 21.Dancette
B
12

This is variation of tachyonflux's answer:

Try to change buttonTint:

<style name="my_checkbox_style">
    <item name="buttonTint">#fff</item>
</style>

<CheckBox
            android:id="@+id/my_checkbox"
            style="@style/my_checkbox_style"
            android:background="#000" />
Backbreaker answered 3/11, 2016 at 9:37 Comment(1)
I have both the buttonTint and the colorAccent items in my style. Works exactly as expected. Thanks.Jariah
L
8

From XML

<androidx.appcompat.widget.AppCompatCheckBox
    ...
    app:buttonTint="@android:color/white" />

OR From Java/Kotlin:

CompoundButtonCompat.setButtonTintList(cbCheck, ColorStateList.valueOf(Color.WHITE));
Lamee answered 31/1, 2018 at 13:24 Comment(0)
Z
7

easily we can change checkbox color by using this property in xml

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/COLOR_WHICH_YOU_WANT" />
Zanze answered 24/5, 2017 at 9:36 Comment(0)
F
1
  1. android:buttonTint="@color/white" this line of your button color change

              <CheckBox
                android:id="@+id/checkk"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/_40sdp"
                android:layout_alignParentLeft="true"
                android:background="@color/black"
                android:buttonTint="@color/white"                 
                android:textSize="@dimen/_14sdp" />
    
Fermentative answered 21/6, 2018 at 10:8 Comment(0)
T
1

we can change checkbox color using singe line of code

android:buttonTint="@color/app_color" //whatever color
Teniers answered 18/8, 2018 at 6:15 Comment(0)
P
1

With MaterialCheckBox

<com.google.android.material.checkbox.MaterialCheckBox
style="@style/checkbox_inv"
android:id="@+id/checkbox"
app:useMaterialThemeColors="false"/>

and in stlyles.xml

<style name="checkbox_inv">
    <item name="checkedIconTint">@color/blue_gray</item>
</style>
Po answered 29/9, 2022 at 13:51 Comment(0)
B
0

Try this

<CheckBox
    android:textSize="15sp"
    android:textColor="#fff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save Loging "
    android:id="@+id/checkBox"
    android:layout_below="@id/PasswordeditText"
    android:checked="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:button="@android:drawable/checkbox_on_background" />

if CheckBox checked, else in android:button= write "@android:drawable/checkbox_off_background"

Backslide answered 5/8, 2015 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.