Artur A
Artur A

Reputation: 297

Is it possible to change TextInputLayout colors programmatically without using theme?

I use this in my theme now I am wondering if possible without using theme either programmatically changing colors of textinputlayout or I can change in xml still without using theme, as I need to fetch colors dynamically and I write code for that or do databinding in xml.

        <item name="colorControlActivated">@color/primaryTextColor</item>
        <item name="android:textColorHint">@color/primaryTextColor</item>
        <item name="android:textColor">@color/primaryTextColor</item>
        <item name="android:editTextColor">@color/primaryTextColor</item>```

Upvotes: 1

Views: 168

Answers (1)

Khaled Qasem
Khaled Qasem

Reputation: 929

Yes, we can use setBackgroundColor().

But in your case I think you need to build your own style, here is an example:

Creating TextInputLayout Theme Let's define a couple of text Styles that will be used as part of the theme. These styles correspond to the different texts used, such as the Hint and Error texts.

<style name="ErrorText" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@android:color/red</item>
    <item name="android:textSize">16sp</item>
</style>

<style name="HintText" parent="TextAppearance.AppCompat">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">14sp</item>
</style>

Then, we create our TextInputLayout theme, referencing the above:

<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
    <!-- reference our hint & error styles -->
    <item name="hintTextAppearance">@style/HintText</item>
    <item name="errorTextAppearance">@style/ErrorText</item>
    <item name="android:textColor">@color/user_input_color</item>
    <item name="android:textColorHint">@color/unfocused_color</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="colorControlActivated">@color/blue</item>
    <item name="colorControlHighlight">@color/green</item>
</style>

And for the last step, we build our layout.xml using the android:theme attribute with the style we defined above:

<android.support.design.widget.TextInputLayout
    android:theme="@style/TextInputLayoutAppearance"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

     <android.support.design.widget.TextInputEditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="@string/hint_email"/>

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

Upvotes: 1

Related Questions