I'm facing a problem regarding checkbox colors. I have to colorize two checkboxes (red and green). They are in a single XML with a LinearLayout. If I compile my app to run on API > 21, everything goes fine:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/touch_selector">
<TextView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="left"
android:textSize="18sp"
android:text="TextView" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="right"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBoxRep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_gravity="center"
android:gravity="right"
android:focusableInTouchMode="false"
android:text=""
android:buttonTint="@color/qs_red"
android:theme="@style/checkBoxStyle" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_gravity="center"
android:gravity="right"
android:focusableInTouchMode="false"
android:text=""
android:buttonTint="@color/qs_green"
android:theme="@style/checkBoxStyle" />
</LinearLayout>
</LinearLayout>
But this code does not work on devices pre-Lollipop. So, doing some research I found this question How to change the color of a CheckBox? So, the solution would be to change the checkbox to AppCompatCheckbox. But, when I do that, I face the following error:
Error inflating class android.support.v7.widget.AppCompatCheckBox
So, I believe my Activity must be a AppCompat one.
The question is: I need to keep only one code and have to dinamically solve this issue. The layout problem could be solved creating two XMLs in two folders (layout and layout-v21). But, how to solve the Java part? Any simple way to solve it without creating two activity classes or something like this?
Thank you.