1

I have created a custom check box and a custom style for it. But when I add color or radius to the check box, it applies the color or radius to the text of the check box as well as to the box itself.

How can I add radius to the check box only and not to the text in my custom Java class (that extends CheckBox) or in the style?

The Vee
  • 11,420
  • 5
  • 27
  • 60
Amey Jahagirdar
  • 455
  • 1
  • 4
  • 14

1 Answers1

0

You can use custom drawable for CheckBox image like in this answer:

<?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>

Make sure your checkbox is like this

android:button="@drawable/checkbox_selector"

<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" />

or that tutorial.

Community
  • 1
  • 1
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • 2
    Adding custom drawable in the `android:button` did not work for me in Material Checkbox version `1.3.0` . I had to set `android:drawable="@drawable/checkbox_selector` instead and also set `android:button="@null` . You can also add `android:drawablePadding` to make it look good. – Ali Akber May 21 '21 at 05:56