5

I am developing an application which has multiple form, but i want some effects on it. What i want to do is changing the color of icon which is placed inside edit text.

<EditText
    android:id="@+id/marks"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/button1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="10dp"
    android:drawableRight="@drawable/ic_border"
    android:hint="Total Marks"
    android:inputType="number"
    android:textColor="#fff" />
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Kriti Jain
  • 63
  • 2
  • 10

5 Answers5

2

Use setTint, setTintMode methods from the DrawableCompat class to set tint for drawable programmatically.

Drawable drawable = R.drawable.image; //Your drawable image
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, Color.GREEN); // Set whatever color you want
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

And after set the drawable for the editText:

editText.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);

EDIT 1:

Make change in Drawable line as below.

Drawable drawable = getResources().getDrawable(R.drawable.ic_done);

EDIT 2

Use Focus Change Listener of Edit Text.

edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
      if(hasFocus){
         Drawable drawable = R.drawable.image; //Your drawable image
         drawable = DrawableCompat.wrap(drawable);
         DrawableCompat.setTint(drawable, Color.GREEN); // Set whatever color you want
         DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);
         editText.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
      }else { 
     }
    }
  });
Vega
  • 27,856
  • 27
  • 95
  • 103
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
0

Try this lib: https://github.com/jrvansuita/IconHandler

Calling:

Icon.on(yourEditText).icon(R.mipmap.your_icon).position(Gravity.LEFT).color(R.color.youColor).put();
Vansuita Jr.
  • 1,949
  • 17
  • 18
0
   <androidx.appcompat.widget.AppCompatEditText
                android:layout_width="0dp"
                android:drawableStart="@drawable/ic_language"
                android:drawableTint="@color/gray"
                android:layout_height="wrap_content"/>

Have you try "android:drawableTint" yet?

siwarak
  • 177
  • 13
0

Try This:

Drawable d1 = getResources().getDrawable(R.drawable.ic_icon);
Drawable drawable = DrawableCompat.wrap(d1);

DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), R.color.color_custom));

editText.setCompoundDrawables(null, null, drawable, null);
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43
-1

Try this..

EditText editText = (EditText) v.findViewById(R.id.myEditText);
Drawable d = mFriendsFeedButton.getCompoundDrawables()[0];

Drawable wrappedDrawable = DrawableCompat.wrap(d);
DrawableCompat.setTint(wrappedDrawable, Color.RED);

editText.setCompoundDrawablesWithIntrinsicBounds(wrappedDrawable, null, null, null);
Rohit Suthar
  • 2,635
  • 1
  • 22
  • 27