XxPixX
XxPixX

Reputation: 33

How to override CheckBox color using AppCompat

When my checkbox is checked, I want the color to be @color/colorPrimaryLight. I can set it that way for Lollipop by setting

android:buttonTint="@color/colorPrimaryLight"

in the CheckBox but I don't know how to do it for anything before Lollipop without changing

    <item name="colorControlActivated">@color/white</item>

in my theme.

CheckBox for Lollipop:

<CheckBox
android:buttonTint="@color/colorPrimaryLight"
android:id="@+id/SwapCheckbox"
android:layout_marginTop="@dimen/margin_top"
android:layout_marginLeft="@dimen/margin_left_right"
android:layout_marginRight="@dimen/margin_left_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

and for anything before Lollipop (and higher than 4.0.3):

<CheckBox
android:id="@+id/SwapCheckbox"
android:layout_marginTop="@dimen/margin_top"
android:layout_marginLeft="@dimen/margin_left_right"
android:layout_marginRight="@dimen/margin_left_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

And I also have in the theme for my app:

<style name="AppTheme" parent="Theme.AppCompat.Light">
//...
<item name="colorControlActivated">@color/white</item>
//...
</style>

I can't change colorControlActivated as it must be white for the indicator in my ViewPager. Is there anyway to change the background color of my checkbox (even if it is to the default Holo style however best so that it is set to @color/colorPrimaryLight)?

Upvotes: 3

Views: 6860

Answers (1)

Prasad
Prasad

Reputation: 3562

Use the following property in the App compat theme like below

<style name="AppTheme" parent="Theme.AppCompat.Light">
  <item name="colorAccent">@color/colorAccent</item>
</style>

and use the android.support.v7.widget.AppCompatCheckBox instead of CheckBox

Upvotes: 8

Related Questions