6

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Asset Bekbossynov
  • 1,633
  • 3
  • 13
  • 25

1 Answers1

10

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>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Can you explain how to work with `setStartIconTintList()` – Asset Bekbossynov Sep 12 '19 at 05:44
  • @AssetBekbossynov This method applies a tint to the start icon drawable as the `app:startIconTint` in the xml or `....` in the style. You can use a single color or you can use a `ColorStateList` to define different colors in the different states. – Gabriele Mariotti Sep 12 '19 at 05:48
  • 3
    Thank you, for answer. I solved it using `valueOf(color: Int)` method of `ColorStateList`. Final version looks like that `textInputLayout.setStartIconTintList(ColorStateList.valueOf(resources.getColor(R.color.color_name)));` – Asset Bekbossynov Sep 12 '19 at 06:02