Francesco Baldan Francesco Baldan - 1 month ago 8
Android Question

How to change TextField and Checkboxes color when they have focus/they are checked?

I have a small problem with colors for few elements in my simple application.
As you can see in the image the TextField, when it has focus, and checkboxes, when they are checked, are green, which is the default color.

Screenshot

This is the code:

<android.support.design.widget.TextInputLayout
android:id="@+id/input_name_label"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="@string/home_text_field"
android:ems="10"
android:id="@+id/input_name"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin" />

</android.support.design.widget.TextInputLayout>


and

<CheckBox
android:text="@string/argInformatica"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scelta3"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_below="@+id/scelta2"
android:layout_centerHorizontal="true"
android:visibility="visible" />


For the checkboxes I found to add this line to the xml tag

android:buttonTint="@color/colorAccent"


but it doesn't work as I would like, the checkbox will have accent color also if it is not checked.

Any idea? Thanks in advance

Answer

Have this style in your styles.xml:

<style name="inputColors">
    <item name="colorControlActivated">@color/thirdColor</item> <!-- In focus -->
    <item name="colorControlNormal">#9e9e9e</item> <!-- not touched -->
</style>

Then, to your CheckBox/EditText, add:

android:theme="@style/inputColors"

It should work