Reputation: 712
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>
Upvotes: 6
Views: 821
Reputation: 2643
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.
Upvotes: 3
Reputation: 2626
Take a look at the custom CheckBox it works great for all version pre/post lollipop.
CustomCheckBox.java:
public class CustomCheckBox extends CheckedTextView {
private Drawable btnDrawable;
public CustomCheckBox(Context context) {
this(context, null);
}
public CustomCheckBox(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
colorAccordingToTheme();
}
@Override
public void setCheckMarkDrawable(Drawable d) {
super.setCheckMarkDrawable(d);
btnDrawable = d;
}
@Override
public void toggle() {
super.toggle();
colorAccordingToTheme();
}
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
colorAccordingToTheme();
}
private void colorAccordingToTheme() {
if (btnDrawable != null) {
btnDrawable.setColorFilter(yourColor, PorterDuff.Mode.SRC_IN);
}
}
}
In xml layout:
<?xml version="1.0" encoding="utf-8"?>
<com.yourpaackcge.CustomCheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:checkMark="@drawable/selector_check_box"
android:gravity="center|left"
android:paddingRight="24dp"
android:paddingLeft="24dp"
android:background="@drawable/ripple"/>
My selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_check_box_on" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_check_box_on" android:state_checked="true"/>
<item android:drawable="@drawable/ic_check_box_off"/>
</selector>
Upvotes: 0