Ali Nasir
Ali Nasir

Reputation: 217

Checkbox color change android

I have a checkbox and i want to change its color. I am attaching a pic for more understanding. enter image description here

I want to change the black squares color to white just like the text "Press to skip". My xml code is below:

<CheckBox
 android:id="@+id/checkBox1"
 android:background="@drawable/tutorial_screen_edit_text_style"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="onCheckboxClicked"
 android:text="Press to Skip"
 android:textColor="@color/white"

 android:layout_alignParentTop="true"
 android:layout_alignParentRight="true"
 android:layout_alignParentEnd="true" />

Upvotes: 3

Views: 4068

Answers (4)

Saim Mahmood
Saim Mahmood

Reputation: 446

The answer is very simple, check this code snippet below:

<CheckBox
android:id="@+id/checkBox1"
android:background="@drawable/tutorial_screen_edit_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="Press to Skip"
android:buttonTint="@color/white"    <!--just add this line-->
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

Upvotes: 6

Mr. Mad
Mr. Mad

Reputation: 1238

Try this one.

     <CheckBox
                        android:id="@+id/chk_remember_signup"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:buttonTint="@android:color/white"
                        android:text="hello" />

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

try using ColorStateList

    ColorStateList  colorStateList = new ColorStateList(
        new int[][]{
                new int[]{android.R.attr.state_checked}, // unchecked
                new int[]{android.R.attr.state_checked} , // checked
        },
        new int[]{
                Color.parseColor("#000000"),  //unchecked color
                Color.parseColor("#FFFFFF"),  //checked color
        }
);

set the color using: setButtonTintList()

    CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
 CompoundButtonCompat.setButtonTintList(cb,colorStateList);

Upvotes: 3

sadat
sadat

Reputation: 4342

define your own drawalbe in drawalbe folder

checkbox_selector.xml

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

your_layout.xml

<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:button="@drawable/checkbox_selector"
    android:text="CheckBox"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/Black" />

Upvotes: 0

Related Questions