0

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.

Community
  • 1
  • 1
Laerte
  • 7,013
  • 3
  • 32
  • 50
  • The thing is once you are using an appcompat library you need to use an appcompat activity as well. – Rod_Algonquin Jun 07 '16 at 21:20
  • Yes, but is there any easy way to do this or will I have to create two separated classes of the same activity and chose between them at running time? – Laerte Jun 07 '16 at 21:35
  • That is not a good design to implement. Having 2 activity means having two different source code that does the same thing. – Rod_Algonquin Jun 07 '16 at 21:40
  • But how do I solve this? Do I have to keep only AppCompat code and use it on newer Android versions too? – Laerte Jun 07 '16 at 21:47
  • Yes you need to convert to appcompat and do small change if it has some. Basically appcompat will run on pre-lollipop and above. – Rod_Algonquin Jun 07 '16 at 21:51

0 Answers0