Search code examples
androidmaterial-designandroid-textinputlayoutmaterial-components-androidmaterial-components

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.


Solution

  • 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>