Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background of Android checkBox?

I have android checkBox and the default background is transparent, I want it to be white so I use style:

<style name="BrandedCheckBox" parent="AppTheme">
    <item name="colorAccent">@color/cyan</item>
    <item name="colorControlNormal">@color/text_gray</item>
    <item name="colorControlActivated">@color/cyan</item>
</style>

and set checkBox theme:

<CheckBox
    android:id="@+id/check_payable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_gravity="center"
    android:theme="@style/BrandedCheckBox"/>

But the result is this: enter image description here But I want it to to be like this: enter image description here

Can any one help me on this?

like image 217
Nazila Avatar asked Jan 30 '17 06:01

Nazila


People also ask

How can I change CheckBox background in android?

This example demonstrates how do I change the color of the check box in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change the default CheckBox color in android?

Setting the android:buttonTint="@color/mybrown" is an easy way to change the box color.


3 Answers

you can use android:button

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

checkbox_background.xml

<?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/checkbox_checked" />
<item android:state_checked="false"
    android:drawable="@drawable/checkbox_unchecked" />
</selector>

I'm Using these two images in drawable

checkbox_unchecked

checkbox_checked

like image 86
Rudresh Avatar answered Oct 18 '22 15:10

Rudresh


Just use a selector drawable defining the exact look you want for each state (the drawables listed in the selector can have transparencies if you need it). Then also ensure you set this in the xml of your checkbox:

android:background="@drawable/checkbox_background"
android:button="@null"

And to avoid some recent bugs of AppCompat Checkbox I recommend you to stick with the good old android.widget.CheckBox (you should use full path to ensure you will not use AppCompat one).

In a short future you will be able to migrate to AppCompatCheckBox from androidX. However at this moment (while I am writing this) it still have bugs with custom backgrounds when running in old api levels. I already reported one here: https://issuetracker.google.com/issues/120865686

I hope my answer is useful for you.

like image 33
Blackd Avatar answered Oct 18 '22 14:10

Blackd


white color code #FFFFFF in your color.xml

<style name="BrandedCheckBox" parent="AppTheme">
    <item name="colorAccent">@color/cyan</item>
    <item name="colorControlNormal">@color/text_gray</item>
    <item name="colorControlActivated">@color/white</item>
</style>
like image 34
Amit Ranjan Avatar answered Oct 18 '22 15:10

Amit Ranjan