Search code examples
androidandroid-themeandroid-styles

Style is not applying in custom checkbox and radiobutton


I am developing custom checkbox and radio button but style is not applying for both in pre lollipop devices (Showing black color instead). I have coded like this :

XML :

<com.kaho.myapp.CustomCheckBox
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="CheckBoxText"
  android:textColor="@color/colorPrimary"
  android:theme="@style/SampleTheme"/>

Custom Checkbox :

public class CustomCheckBox extends CheckBox {
    public CustomCheckBox(Context context) {
        super(context);
    }

    public CustomCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFont(context, attrs) ;
    }

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFont(context,attrs) ;
    }

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setFont(context, attrs) ;
    }

    private void setFont(Context context, AttributeSet attrs) {
        if (attrs != null) {
            /* Set the font */
        }
    }
}

Font in setting properly . Style :

<style name="SampleTheme" parent="Theme.AppCompat.Light">
    <item name="colorAccent">#08c283</item>
    <item name="android:textColorSecondary">#969696</item>
</style>

Solution

  • You have this problem because pre-Lollipop devices don't have the possibility to set a colorAccent by default. To obtain a behaviour like this extend your view from the corresponding support view. There would be something like this:

    public class CustomCheckBox extends AppCompatCheckBox
    public class CustomRadioButton extends AppCompatRadioButton
    

    In this way, your views will have the material design style on pre Lollipop devices.