Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 562
Nikesh Kedlaya Avatar asked Jul 15 '16 08:07

Nikesh Kedlaya


People also ask

Can you style an HTML checkbox button to look like a radio?

Just replace type=radio with type=checkbox in above CSS and see how normal looking html checkbox changes into pushbutton checkbox.

How do you check if Radiobutton is checked or not?

To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.

What is the difference between Radiobutton and checkbox in HTML?

Checkboxes allow the user to choose items from a fixed number of alternatives, while radio buttons allow the user to choose exactly one item from a list of several predefined alternatives.


1 Answers

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.

like image 90
Iulian Popescu Avatar answered Oct 14 '22 00:10

Iulian Popescu