Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout set color of startIconDrawable programmaticaly

I want to set start icon for my TextInputLayout programmatically, but I faced problem with his color. When I set drawable, its color becomes gray, but original color of it is orange. I know that I can change its color by using startIconTint parameter in xml, but I want to change its color programmatically. Can someone help me with this.

like image 201
Asset Bekbossynov Avatar asked Sep 12 '19 05:09

Asset Bekbossynov


People also ask

How do I change the color of my TextInputLayout outline?

Hint color could be set via “android:textColorHint” parameter of TextInputLayout. This parameter also changes the label default color (label focused color could also be changed in other ways). Bottom line color could be changed with “app:backgroundTint” attribute of EditText view. colors.

Why use TextInputLayout?

The primary use of a TextInputLayout is to act as a wrapper for EditText(or its descendant) and enable floating hint animations. Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.


1 Answers

Just use the methods setStartIconDrawable and setStartIconTintList:

textInputLayout.setStartIconDrawable(...);
textInputLayout.setStartIconTintList(ContextCompat.getColorStateList(this,R.color.text_input_selector));

You can use a color or a color selector.
Something like:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_activated="true"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.54" android:color="?attr/colorOnSurface"/>
</selector>
like image 83
Gabriele Mariotti Avatar answered Sep 21 '22 21:09

Gabriele Mariotti