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>
Just replace type=radio with type=checkbox in above CSS and see how normal looking html checkbox changes into pushbutton checkbox.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With